Skip to content

Commit

Permalink
Also lint on ! (not) unary operator in the ambiguous_precedence lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Oct 26, 2023
1 parent c9d8201 commit a366196
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/precedence.rs
Expand Up @@ -68,7 +68,7 @@ impl EarlyLintPass for Precedence {
}

if let ExprKind::Unary(unop, operand) = &expr.kind
&& matches!(unop, UnOp::Neg)
&& matches!(unop, UnOp::Neg | UnOp::Not)
&& let ExprKind::MethodCall(..) = operand.kind
{
let mut arg = operand;
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/lint/precedence.fixed
Expand Up @@ -18,6 +18,7 @@ fn main() {
//~^ WARN operator precedence can trip the unwary
let _ = (1 + 2) << (3 + 1);
//~^ WARN operator precedence can trip the unwary

let _ = -(1i32.abs());
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -(1f32.abs());
Expand All @@ -39,11 +40,19 @@ fn main() {
let _ = -(1f64.sin().sin());
//~^ WARN unary operator `-` has lower precedence than method call

let _ = !(10_i32.abs());
//~^ WARN unary operator `!` has lower precedence than method call
let _ = !(10_i32.abs().abs());
//~^ WARN unary operator `!` has lower precedence than method call

// These should not trigger an error
let _ = (-1i32).abs();
let _ = (-1f32).abs();
let _ = -(1i32).abs();
let _ = -(1f32).abs();
let _ = -(1i32.abs());
let _ = -(1f32.abs());
let _ = (!10_i32).abs();
macro_rules! number { () => { 5f32 }; }
let _ = !number!().is_nan();
}
9 changes: 9 additions & 0 deletions tests/ui/lint/precedence.rs
Expand Up @@ -18,6 +18,7 @@ fn main() {
//~^ WARN operator precedence can trip the unwary
let _ = 1 + 2 << 3 + 1;
//~^ WARN operator precedence can trip the unwary

let _ = -1i32.abs();
//~^ WARN unary operator `-` has lower precedence than method call
let _ = -1f32.abs();
Expand All @@ -39,11 +40,19 @@ fn main() {
let _ = -1f64.sin().sin();
//~^ WARN unary operator `-` has lower precedence than method call

let _ = !10_i32.abs();
//~^ WARN unary operator `!` has lower precedence than method call
let _ = !10_i32.abs().abs();
//~^ WARN unary operator `!` has lower precedence than method call

// These should not trigger an error
let _ = (-1i32).abs();
let _ = (-1f32).abs();
let _ = -(1i32).abs();
let _ = -(1f32).abs();
let _ = -(1i32.abs());
let _ = -(1f32.abs());
let _ = (!10_i32).abs();
macro_rules! number { () => { 5f32 }; }
let _ = !number!().is_nan();
}
44 changes: 33 additions & 11 deletions tests/ui/lint/precedence.stderr
Expand Up @@ -88,7 +88,7 @@ LL | let _ = (1 + 2) << (3 + 1);
| + + + +

warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:21:13
--> $DIR/precedence.rs:22:13
|
LL | let _ = -1i32.abs();
| ^^^^^^^^^^^
Expand All @@ -99,7 +99,7 @@ LL | let _ = -(1i32.abs());
| + +

warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:23:13
--> $DIR/precedence.rs:24:13
|
LL | let _ = -1f32.abs();
| ^^^^^^^^^^^
Expand All @@ -110,7 +110,7 @@ LL | let _ = -(1f32.abs());
| + +

warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:25:13
--> $DIR/precedence.rs:26:13
|
LL | let _ = -1f64.asin();
| ^^^^^^^^^^^^
Expand All @@ -121,7 +121,7 @@ LL | let _ = -(1f64.asin());
| + +

warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:27:13
--> $DIR/precedence.rs:28:13
|
LL | let _ = -1f64.asinh();
| ^^^^^^^^^^^^^
Expand All @@ -132,7 +132,7 @@ LL | let _ = -(1f64.asinh());
| + +

warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:29:13
--> $DIR/precedence.rs:30:13
|
LL | let _ = -1f64.tan();
| ^^^^^^^^^^^
Expand All @@ -143,7 +143,7 @@ LL | let _ = -(1f64.tan());
| + +

warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:31:13
--> $DIR/precedence.rs:32:13
|
LL | let _ = -1f64.tanh();
| ^^^^^^^^^^^^
Expand All @@ -154,7 +154,7 @@ LL | let _ = -(1f64.tanh());
| + +

warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:33:13
--> $DIR/precedence.rs:34:13
|
LL | let _ = -1.0_f64.cos().cos();
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -165,7 +165,7 @@ LL | let _ = -(1.0_f64.cos().cos());
| + +

warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:35:13
--> $DIR/precedence.rs:36:13
|
LL | let _ = -1.0_f64.cos().sin();
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -176,7 +176,7 @@ LL | let _ = -(1.0_f64.cos().sin());
| + +

warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:37:13
--> $DIR/precedence.rs:38:13
|
LL | let _ = -1.0_f64.sin().cos();
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -187,7 +187,7 @@ LL | let _ = -(1.0_f64.sin().cos());
| + +

warning: unary operator `-` has lower precedence than method call
--> $DIR/precedence.rs:39:13
--> $DIR/precedence.rs:40:13
|
LL | let _ = -1f64.sin().sin();
| ^^^^^^^^^^^^^^^^^
Expand All @@ -197,5 +197,27 @@ help: consider adding parentheses to clarify your intent
LL | let _ = -(1f64.sin().sin());
| + +

warning: 18 warnings emitted
warning: unary operator `!` has lower precedence than method call
--> $DIR/precedence.rs:43:13
|
LL | let _ = !10_i32.abs();
| ^^^^^^^^^^^^^
|
help: consider adding parentheses to clarify your intent
|
LL | let _ = !(10_i32.abs());
| + +

warning: unary operator `!` has lower precedence than method call
--> $DIR/precedence.rs:45:13
|
LL | let _ = !10_i32.abs().abs();
| ^^^^^^^^^^^^^^^^^^^
|
help: consider adding parentheses to clarify your intent
|
LL | let _ = !(10_i32.abs().abs());
| + +

warning: 20 warnings emitted

0 comments on commit a366196

Please sign in to comment.