Rollup merge of #121364 - Urgau:unary_precedence, r=compiler-errors
Implement lint against ambiguous negative literals This PR implements a lint against ambiguous negative literals with a literal and method calls right after it. ## `ambiguous_negative_literals` (deny-by-default) The `ambiguous_negative_literals` lint checks for cases that are confusing between a negative literal and a negation that's not part of the literal. ### Example ```rust,compile_fail -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1 ``` ### Explanation Method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs. <details> <summary>Old proposed lint</summary> ## `ambiguous_unary_precedence` (deny-by-default) The `ambiguous_unary_precedence` lint checks for use the negative unary operator with a literal and method calls. ### Example ```rust -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1 ``` ### Explanation Unary operations take precedence on binary operations and method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs. </details> ----- Note: This is a strip down version of https://github.com/rust-lang/rust/pull/117161, without the binary op precedence. Fixes https://github.com/rust-lang/rust/issues/117155 `@rustbot` labels +I-lang-nominated cc `@scottmcm` r? compiler
This commit is contained in:
commit
ae71900ef6
12 changed files with 325 additions and 156 deletions
|
@ -5,6 +5,11 @@ lint_ambiguous_glob_reexport = ambiguous glob re-exports
|
|||
.label_first_reexport = the name `{$name}` in the {$namespace} namespace is first re-exported here
|
||||
.label_duplicate_reexport = but the name `{$name}` in the {$namespace} namespace is also re-exported here
|
||||
|
||||
lint_ambiguous_negative_literals = `-` has lower precedence than method calls, which might be unexpected
|
||||
.example = e.g. `-4.abs()` equals `-4`; while `(-4).abs()` equals `4`
|
||||
.negative_literal = add parentheses around the `-` and the literal to call the method on a negative literal
|
||||
.current_behavior = add parentheses around the literal and the method call to keep the current behavior
|
||||
|
||||
lint_ambiguous_wide_pointer_comparisons = ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
|
||||
.addr_metadata_suggestion = use explicit `std::ptr::eq` method to compare metadata and addresses
|
||||
.addr_suggestion = use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue