-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add lint to check for temporary where dropping has side effects. #3066
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 to check for temporary where dropping has side effects. #3066
Conversation
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.
I only have a small suggestion to improve the docs, someone else should probably have a look, too. In general the code looks good to me 👍
|
||
/// **What it does:** | ||
/// Checks for temporaries containing values where dropping has a side effect. | ||
/// |
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.
I think it would be good to include a list of links to the stdlib to the different types this lint checks.
@@ -0,0 +1,152 @@ | |||
// #![cfg_attr(feature = "clippy", deny(dropping_temporary_with_side_effect))] |
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.
leftover comment
It's not a bug. A I have a suggestion that makes this lint clearer: print the type in the diagnostic so the user knows what we are complaining about, not just where. |
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.
All my comments are probably me misunderstanding something about the nature of the wrongness. Can you elaborate on them (maybe even with a comment in the test as to why these are wrong and should be linted?)
|
||
declare_clippy_lint! { | ||
pub DROPPING_TEMPORARY_WITH_SIDE_EFFECT, | ||
correctness, |
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.
we should have this in the nursery first and run it on a bunch of large crates (especially ones with paralellism)
TypeVariants::TyForeign(_) | ||
| TypeVariants::TyFnDef(_, _) | ||
| TypeVariants::TyDynamic(_, _) | ||
=> false, // Type of a function, trait or a forein type |
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.
nit: foreign
{ | ||
let mut errors = crate::utils::conf::ERRORS.lock() | ||
.expect("no threading here"); | ||
errors.push(crate::utils::conf::Error::Toml(e.to_string())); |
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 previous code seemed fine
|
||
{ | ||
let global_errors = ERRORS.lock().expect("no threading -> mutex always safe"); | ||
assert!(global_errors.is_empty()); |
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.
original code seemed fine, too
{ | ||
let mut global_errors = ERRORS.lock().expect("no threading -> mutex always safe"); | ||
errors = global_errors.split_off(0); | ||
} // Release the lock for global_errors |
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.
was also fine
error: Expression results in an anonymous temporary variable that will have side effects when being dropped. | ||
--> $DIR/temporary_dropped_with_sideeffects.rs:78:30 | ||
| | ||
78 | let _a4 = MutexIndexable{}[&mutex.lock().unwrap()]; // Should trigger |
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.
seems ok
error: Expression results in an anonymous temporary variable that will have side effects when being dropped. | ||
--> $DIR/temporary_dropped_with_sideeffects.rs:85:6 | ||
| | ||
85 | if *mutex_bool.lock().unwrap() { // Should trigger |
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.
this is ok code
error: Expression results in an anonymous temporary variable that will have side effects when being dropped. | ||
--> $DIR/temporary_dropped_with_sideeffects.rs:89:8 | ||
| | ||
89 | match mutex_bool.lock() { // should trigger |
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.
also ok
error: Expression results in an anonymous temporary variable that will have side effects when being dropped. | ||
--> $DIR/temporary_dropped_with_sideeffects.rs:107:9 | ||
| | ||
107 | match cache.read() { // Should trigger |
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.
also ok
TypeVariants::TyClosure(_, _) | ||
| TypeVariants::TyFnPtr(_) | ||
| TypeVariants::TyGenerator(_, _, _) | ||
| TypeVariants::TyGeneratorWitness(_) // Question for the reviewer: is this correct? |
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.
yea let's ignore those cases
Ping from triage @PieterPenninckx. Do you want to continue working on this? |
@flip1995 Thanks for asking. I am now busy with other things and I will probably not work on this in the coming months. Should I close this PR and re-open it when (and if) I resume work on it? |
No problem! Thanks for your contribution. Feel free to reopen, whenever you want to continue to work on this. |
Closes #1904 .
There are still some things unclear to me; I indicated this in the source code with questions for the reviewer.
The "dogfood" test also gives one error on the existing code:
I don't know enough about the compiler to see if this uncovered an actual bug or not (I guess not). If this does not indicate a real bug in
clippy_lints/src/consts.rs
, I can fix it easily.Thank to @oli-obk for his help on this lint at RustFest Paris. As you can see, it took a lot longer than one workshop to write this lint.