Skip to content

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

Conversation

PieterPenninckx
Copy link

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:

error: Expression results in an anonymous temporary variable that will have side effects when being dropped.
   --> clippy_lints/src/consts.rs:444:33
    |
444 |                       let alloc = tcx
    |  _________________________________^
445 | |                         .alloc_map
446 | |                         .lock()
    | |_______________________________^
    |
    = note: #[deny(dropping_temporary_with_side_effect)] on by default
    = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#dropping_temporary_with_side_effect

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.

Copy link
Member

@phansch phansch left a 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.
///
Copy link
Member

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))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover comment

@oli-obk
Copy link
Contributor

oli-obk commented Aug 21, 2018

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.

It's not a bug. A lock() function is always something that is just meant to create a short temporary handle.

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.

Copy link
Contributor

@oli-obk oli-obk left a 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,
Copy link
Contributor

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
Copy link
Contributor

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()));
Copy link
Contributor

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());
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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?
Copy link
Contributor

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

@phansch phansch added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Sep 2, 2018
@flip1995
Copy link
Member

Ping from triage @PieterPenninckx. Do you want to continue working on this?

@PieterPenninckx
Copy link
Author

@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?

@flip1995
Copy link
Member

No problem! Thanks for your contribution. Feel free to reopen, whenever you want to continue to work on this.

@flip1995 flip1995 closed this Dec 18, 2018
@flip1995 flip1995 added S-inactive-closed Status: Closed due to inactivity and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Dec 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive-closed Status: Closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint request: warn when temporaries are used for values for which dropping has side effects
4 participants