mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
ae71900ef6
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 |
||
---|---|---|
.. | ||
src | ||
Cargo.toml | ||
messages.ftl |