-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add suppress_restriction_lint_in_const
config
#9920
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
96f1385
add `suppress_lint_in_const` conf
naosense aed9497
add test and stderr
naosense 4528aec
update config and suggest
naosense c9bf4b7
resolve conflicts
naosense 1fc98c5
change default value
naosense 67a9413
change note style
naosense eec5039
fix test
naosense File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
suppress-restriction-lint-in-const = true |
This file contains hidden or 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,60 @@ | ||
#![feature(inline_const)] | ||
#![warn(clippy::indexing_slicing)] | ||
// We also check the out_of_bounds_indexing lint here, because it lints similar things and | ||
// we want to avoid false positives. | ||
#![warn(clippy::out_of_bounds_indexing)] | ||
#![allow(unconditional_panic, clippy::no_effect, clippy::unnecessary_operation)] | ||
|
||
const ARR: [i32; 2] = [1, 2]; | ||
const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. | ||
const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. | ||
|
||
const fn idx() -> usize { | ||
1 | ||
} | ||
const fn idx4() -> usize { | ||
4 | ||
} | ||
|
||
fn main() { | ||
let x = [1, 2, 3, 4]; | ||
let index: usize = 1; | ||
x[index]; | ||
x[4]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. | ||
x[1 << 3]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. | ||
|
||
x[0]; // Ok, should not produce stderr. | ||
x[3]; // Ok, should not produce stderr. | ||
x[const { idx() }]; // Ok, should not produce stderr. | ||
x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. | ||
const { &ARR[idx()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. | ||
const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. | ||
|
||
let y = &x; | ||
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021 | ||
y[4]; // Ok, rustc will handle references too. | ||
|
||
let v = vec![0; 5]; | ||
v[0]; | ||
v[10]; | ||
v[1 << 3]; | ||
|
||
const N: usize = 15; // Out of bounds | ||
const M: usize = 3; // In bounds | ||
x[N]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. | ||
x[M]; // Ok, should not produce stderr. | ||
v[N]; | ||
v[M]; | ||
} | ||
|
||
/// An opaque integer representation | ||
pub struct Integer<'a> { | ||
/// The underlying data | ||
value: &'a [u8], | ||
} | ||
impl<'a> Integer<'a> { | ||
// Check whether `self` holds a negative number or not | ||
pub const fn is_negative(&self) -> bool { | ||
self.value[0] & 0b1000_0000 != 0 | ||
} | ||
} |
This file contains hidden or 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,70 @@ | ||
error[E0080]: evaluation of `main::{constant#3}` failed | ||
--> $DIR/test.rs:31:14 | ||
| | ||
LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. | ||
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4 | ||
|
||
note: erroneous constant used | ||
--> $DIR/test.rs:31:5 | ||
| | ||
LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: indexing may panic | ||
--> $DIR/test.rs:22:5 | ||
| | ||
LL | x[index]; | ||
| ^^^^^^^^ | ||
| | ||
= help: consider using `.get(n)` or `.get_mut(n)` instead | ||
= note: `-D clippy::indexing-slicing` implied by `-D warnings` | ||
|
||
error: indexing may panic | ||
--> $DIR/test.rs:38:5 | ||
| | ||
LL | v[0]; | ||
| ^^^^ | ||
| | ||
= help: consider using `.get(n)` or `.get_mut(n)` instead | ||
|
||
error: indexing may panic | ||
--> $DIR/test.rs:39:5 | ||
| | ||
LL | v[10]; | ||
| ^^^^^ | ||
| | ||
= help: consider using `.get(n)` or `.get_mut(n)` instead | ||
|
||
error: indexing may panic | ||
--> $DIR/test.rs:40:5 | ||
| | ||
LL | v[1 << 3]; | ||
| ^^^^^^^^^ | ||
| | ||
= help: consider using `.get(n)` or `.get_mut(n)` instead | ||
|
||
error: indexing may panic | ||
--> $DIR/test.rs:46:5 | ||
| | ||
LL | v[N]; | ||
| ^^^^ | ||
| | ||
= help: consider using `.get(n)` or `.get_mut(n)` instead | ||
|
||
error: indexing may panic | ||
--> $DIR/test.rs:47:5 | ||
| | ||
LL | v[M]; | ||
| ^^^^ | ||
| | ||
= help: consider using `.get(n)` or `.get_mut(n)` instead | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/test.rs:10:24 | ||
| | ||
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. | ||
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4 | ||
|
||
error: aborting due to 8 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description sounds perfect :)