-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add lint for using a type with a destructor in a zero-length repeat expr #74857
Closed
Closed
Changes from all commits
Commits
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 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,36 @@ | ||
#![feature(const_generics)] | ||
#![allow(incomplete_features)] | ||
#![deny(zero_repeat_with_drop)] | ||
|
||
const ZERO: usize = 1 * 0; | ||
|
||
const fn make_val<T>(val: T) -> T { val } | ||
|
||
struct NoDropOrCopy; | ||
struct WithDropGlue(String); | ||
|
||
fn foo<T, V: Copy, F: Fn() -> T, const N: usize>(not_copy: F, copy: V) { | ||
// All of these should triger the lint | ||
let _ = [not_copy(); 0]; //~ ERROR used a type | ||
let _ = [not_copy(); 1 - 1]; //~ ERROR used a type | ||
let _ = [not_copy(); ZERO]; //~ ERROR used a type | ||
let _ = [Some(not_copy()); 0]; //~ ERROR used a type | ||
let _ = [None::<T>; 0]; //~ ERROR used a type | ||
let _ = [make_val(not_copy()); 0]; //~ ERROR used a type | ||
let _ = [String::new(); 0]; //~ ERROR used a type | ||
let _ = [WithDropGlue(String::new()); 0]; //~ ERROR used a type | ||
|
||
// None of these should trigger the lint | ||
let _ = [copy; 0]; | ||
let _ = [Some(copy); 0]; | ||
let _ = [NoDropOrCopy; 0]; | ||
let _ = [not_copy(); 1]; | ||
let _ = [copy; N]; | ||
} | ||
|
||
fn allow_it() { | ||
#[allow(zero_repeat_with_drop)] | ||
let _ = [WithDropGlue(String::new()); 1 - 1]; | ||
} | ||
|
||
fn main() {} |
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,79 @@ | ||
error: used a type with a destructor in a zero-length repeat expression | ||
--> $DIR/lint-zero-repeat-with-drop.rs:14:13 | ||
| | ||
LL | let _ = [not_copy(); 0]; | ||
| ^^^^^^^^^^^^^^^ help: consider using an empty array expression: `[]` | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/lint-zero-repeat-with-drop.rs:3:9 | ||
| | ||
LL | #![deny(zero_repeat_with_drop)] | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
= note: the value used here has type `T`, which may have a destructor | ||
= note: a length of zero is used, which will cause the value to be dropped in a strange location | ||
|
||
error: used a type with a destructor in a zero-length repeat expression | ||
--> $DIR/lint-zero-repeat-with-drop.rs:15:13 | ||
| | ||
LL | let _ = [not_copy(); 1 - 1]; | ||
| ^^^^^^^^^^^^^^^^^^^ help: consider using an empty array expression: `[]` | ||
| | ||
= note: the value used here has type `T`, which may have a destructor | ||
= note: a length of zero is used, which will cause the value to be dropped in a strange location | ||
|
||
error: used a type with a destructor in a zero-length repeat expression | ||
--> $DIR/lint-zero-repeat-with-drop.rs:16:13 | ||
| | ||
LL | let _ = [not_copy(); ZERO]; | ||
| ^^^^^^^^^^^^^^^^^^ help: consider using an empty array expression: `[]` | ||
| | ||
= note: the value used here has type `T`, which may have a destructor | ||
= note: a length of zero is used, which will cause the value to be dropped in a strange location | ||
|
||
error: used a type with a destructor in a zero-length repeat expression | ||
--> $DIR/lint-zero-repeat-with-drop.rs:17:13 | ||
| | ||
LL | let _ = [Some(not_copy()); 0]; | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using an empty array expression: `[]` | ||
| | ||
= note: the value used here has type `std::option::Option<T>`, which may have a destructor | ||
= note: a length of zero is used, which will cause the value to be dropped in a strange location | ||
|
||
error: used a type with a destructor in a zero-length repeat expression | ||
--> $DIR/lint-zero-repeat-with-drop.rs:18:13 | ||
| | ||
LL | let _ = [None::<T>; 0]; | ||
| ^^^^^^^^^^^^^^ help: consider using an empty array expression: `[]` | ||
| | ||
= note: the value used here has type `std::option::Option<T>`, which may have a destructor | ||
= note: a length of zero is used, which will cause the value to be dropped in a strange location | ||
|
||
error: used a type with a destructor in a zero-length repeat expression | ||
--> $DIR/lint-zero-repeat-with-drop.rs:19:13 | ||
| | ||
LL | let _ = [make_val(not_copy()); 0]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using an empty array expression: `[]` | ||
| | ||
= note: the value used here has type `T`, which may have a destructor | ||
= note: a length of zero is used, which will cause the value to be dropped in a strange location | ||
|
||
error: used a type with a destructor in a zero-length repeat expression | ||
--> $DIR/lint-zero-repeat-with-drop.rs:20:13 | ||
| | ||
LL | let _ = [String::new(); 0]; | ||
| ^^^^^^^^^^^^^^^^^^ help: consider using an empty array expression: `[]` | ||
| | ||
= note: the value used here has type `std::string::String`, which may have a destructor | ||
= note: a length of zero is used, which will cause the value to be dropped in a strange location | ||
|
||
error: used a type with a destructor in a zero-length repeat expression | ||
--> $DIR/lint-zero-repeat-with-drop.rs:21:13 | ||
| | ||
LL | let _ = [WithDropGlue(String::new()); 0]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using an empty array expression: `[]` | ||
| | ||
= note: the value used here has type `WithDropGlue`, which may have a destructor | ||
= note: a length of zero is used, which will cause the value to be dropped in a strange location | ||
|
||
error: aborting due to 8 previous errors | ||
|
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.
Ideally, we would use the proper spans for these notes, but it doesn't seem worth the effort to pass them through into the built MIR. We could move this to HIR typeck, but it seemed preferrable to keep this repeat-length checking logic in one place.