-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lint negation "followed" by method call #3903
Comments
Yeah, this makes a lot of sense. The precedence rules are perfect for compilers, but people don't necessarily have them ingrained. And code is read much more times than it is written 👍 |
In my mind |
I agree. The original issue was on numeric literals, not on variables. On variables it seems pretty clear. Linting Note that literals other than integers or floats should not be linted. |
Numeric literals currently emit a precedence lint: trait Trait { fn method(&self) -> i32 { 42 } }
impl Trait for &str {}
fn main() {
let a = -42_i32.abs();
let b = -42_f32.abs();
let c = -"".method();
dbg!(a, b, c);
}
Is there anything left to do for this issue? |
Calling
-x.foo()
will first callfoo
and then negate. While this is correct as per the precedence rules, it can be surprising (see rust-lang/rust#59388). We should suggest a cleanup either by writing(-x).foo()
if that was intended, by writing-(x.foo())
for by introducing an intermediate variable:The text was updated successfully, but these errors were encountered: