-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…14115) Commit 2550530 has extended the `precedence` lint to include bitmasking and shift operations. The lint is warn by default, and this generates many hits, especially in embedded or system code, where it is very idiomatic to use expressions such as `1 << 3 | 1 << 5` without parentheses. This commit splits the recent addition into a new lint, which is put into the "restriction" category, while the original one stays in "complexity", because mixing bitmasking and arithmetic operations is less typical. Fix #14097 changelog: [`precedence_bits`]: new lint
- Loading branch information
Showing
8 changed files
with
153 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![warn(clippy::precedence_bits)] | ||
#![allow( | ||
unused_must_use, | ||
clippy::no_effect, | ||
clippy::unnecessary_operation, | ||
clippy::precedence | ||
)] | ||
#![allow(clippy::identity_op)] | ||
#![allow(clippy::eq_op)] | ||
|
||
macro_rules! trip { | ||
($a:expr) => { | ||
match $a & 0b1111_1111u8 { | ||
0 => println!("a is zero ({})", $a), | ||
_ => println!("a is {}", $a), | ||
} | ||
}; | ||
} | ||
|
||
fn main() { | ||
1 << 2 + 3; | ||
1 + 2 << 3; | ||
4 >> 1 + 1; | ||
1 + 3 >> 2; | ||
1 ^ 1 - 1; | ||
3 | 2 - 1; | ||
3 & 5 - 2; | ||
0x0F00 & (0x00F0 << 4); | ||
0x0F00 & (0xF000 >> 4); | ||
(0x0F00 << 1) ^ 3; | ||
(0x0F00 << 1) | 2; | ||
|
||
let b = 3; | ||
trip!(b * 8); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![warn(clippy::precedence_bits)] | ||
#![allow( | ||
unused_must_use, | ||
clippy::no_effect, | ||
clippy::unnecessary_operation, | ||
clippy::precedence | ||
)] | ||
#![allow(clippy::identity_op)] | ||
#![allow(clippy::eq_op)] | ||
|
||
macro_rules! trip { | ||
($a:expr) => { | ||
match $a & 0b1111_1111u8 { | ||
0 => println!("a is zero ({})", $a), | ||
_ => println!("a is {}", $a), | ||
} | ||
}; | ||
} | ||
|
||
fn main() { | ||
1 << 2 + 3; | ||
1 + 2 << 3; | ||
4 >> 1 + 1; | ||
1 + 3 >> 2; | ||
1 ^ 1 - 1; | ||
3 | 2 - 1; | ||
3 & 5 - 2; | ||
0x0F00 & 0x00F0 << 4; | ||
0x0F00 & 0xF000 >> 4; | ||
0x0F00 << 1 ^ 3; | ||
0x0F00 << 1 | 2; | ||
|
||
let b = 3; | ||
trip!(b * 8); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
error: operator precedence might not be obvious | ||
--> tests/ui/precedence_bits.rs:28:5 | ||
| | ||
LL | 0x0F00 & 0x00F0 << 4; | ||
| ^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `0x0F00 & (0x00F0 << 4)` | ||
| | ||
= note: `-D clippy::precedence-bits` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::precedence_bits)]` | ||
|
||
error: operator precedence might not be obvious | ||
--> tests/ui/precedence_bits.rs:29:5 | ||
| | ||
LL | 0x0F00 & 0xF000 >> 4; | ||
| ^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `0x0F00 & (0xF000 >> 4)` | ||
|
||
error: operator precedence might not be obvious | ||
--> tests/ui/precedence_bits.rs:30:5 | ||
| | ||
LL | 0x0F00 << 1 ^ 3; | ||
| ^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `(0x0F00 << 1) ^ 3` | ||
|
||
error: operator precedence might not be obvious | ||
--> tests/ui/precedence_bits.rs:31:5 | ||
| | ||
LL | 0x0F00 << 1 | 2; | ||
| ^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `(0x0F00 << 1) | 2` | ||
|
||
error: aborting due to 4 previous errors | ||
|