Skip to content
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 let_underscore_lock lint #5101

Merged
merged 3 commits into from
Jan 30, 2020
Merged

Conversation

basil-cow
Copy link
Contributor

closes #1574
changelog: add let_underscore_lock lint

I am not entirely sure about my docs/messages wording here, improvements are welcome

@flip1995 flip1995 added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 27, 2020
@JohnTitor
Copy link
Member

I think it'd be nice to separate test files.

@basil-cow
Copy link
Contributor Author

Oh, and I forgot to ask your input about this: is there a good reason to not lint let _ = <expr with MutexGuard/RWGuard ty>?

@flip1995
Copy link
Member

flip1995 commented Jan 28, 2020

is there a good reason to not lint let _ = <expr with MutexGuard/RWGuard ty>?

I cannot come up with one. When a guard is produced in any way, dropping it immediately is probably a mistake.

This can be nicely checked with TyS::walk.

// Pseudocode ahead
expr_ty(init_expr).walk().any(|ty| GUARD_TYPES.any(|gty| match_ty(gty, ty)));

@basil-cow
Copy link
Contributor Author

I just thought, someone might use let _ = mutex_lock to drop the lock prematurely. Maybe we should lint all the functions that return a guard.

@flip1995
Copy link
Member

I just thought, someone might use let _ = mutex_lock to drop the lock prematurely.

In that case, I think drop(mutex_lock) would be way better.

@basil-cow basil-cow force-pushed the let_underscore_lock branch 2 times, most recently from 85a46b3 to 66f698c Compare January 29, 2020 19:37
@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jan 30, 2020
@basil-cow
Copy link
Contributor Author

@flip1995 from the documentation it seems like TyS::walk walks generic parameters on a type recursively, why is it relevant here?

@flip1995
Copy link
Member

Result<_Guard, _> is pretty usual, I think. And we also want to detect this Guard types.

@basil-cow
Copy link
Contributor Author

Oh, that's what you mean, gotcha

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

LGTM, only formatting left to do.

Comment on lines 81 to 87
span_lint_and_help(
cx,
LET_UNDERSCORE_LOCK,
stmt.span,
"non-binding let on a synchronization lock",
"consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`"
)
Copy link
Member

Choose a reason for hiding this comment

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

double indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why doesn't fmt do that?

Copy link
Member

Choose a reason for hiding this comment

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

rustfmt currently only formats in some macros. The if_chain macro is too complex, for rustfmt to format the code in it reliably.

@flip1995 flip1995 added S-waiting-on-bors Status: The marked PR was approved and is only waiting bors and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 30, 2020
@flip1995
Copy link
Member

@bors r+

Travis has currently problems, so I marked it as S-waiting-on-rustup to merge this as soon as travis works again.

@bors
Copy link
Collaborator

bors commented Jan 30, 2020

📌 Commit 63ab7a5 has been approved by flip1995

bors added a commit that referenced this pull request Jan 30, 2020
add `let_underscore_lock` lint

closes #1574
changelog: add `let_underscore_lock` lint

I am not entirely sure about my docs/messages wording here, improvements are welcome
@bors
Copy link
Collaborator

bors commented Jan 30, 2020

⌛ Testing commit 63ab7a5 with merge c13268a...

@bors
Copy link
Collaborator

bors commented Jan 30, 2020

💥 Test timed out

@flip1995
Copy link
Member

@bors retry

@bors
Copy link
Collaborator

bors commented Jan 30, 2020

⌛ Testing commit 63ab7a5 with merge 668bc48...

bors added a commit that referenced this pull request Jan 30, 2020
add `let_underscore_lock` lint

closes #1574
changelog: add `let_underscore_lock` lint

I am not entirely sure about my docs/messages wording here, improvements are welcome
@bors
Copy link
Collaborator

bors commented Jan 30, 2020

☀️ Test successful - checks-travis, status-appveyor
Approved by: flip1995
Pushing 668bc48 to master...

@bors bors merged commit 63ab7a5 into rust-lang:master Jan 30, 2020
@mleduque
Copy link

I see this in my code now.
There are
let _ = SHARED_OBJECT.lock().map(|content| { ... });

Is that wrong ?

@flip1995

This comment has been minimized.

@flip1995
Copy link
Member

Hm thinking more about this: With your map(..) call, content is not the thing inside the Mutex, but the MutexGuard itself. So your type is probably (?):

let _ = SHARED_OBJECT.lock().map(|content: MutexGuard<impl Iterator<Item = _>>| { ... });

From the lock documentation:

If another user of this mutex panicked while holding the mutex, then this call will return an error once the mutex is acquired.

So mapping the result of the lock() call only really makes sense, if you do some error handling (other thread paniced) for the above case in there. But I wouldn't do error handling in a map call, but rather in a match _.lock() { Ok(_) => { ... }, Err(_) => panic!("..."),. Or you do

SHARED_OBJECT.lock().expect("..").map(|content: <Iterator>::Item>| { ... })

@mleduque
Copy link

I had to check, I didn't write the code, it was just pointed to me by clippy (that's totally the point of clippy, eh! Thanks by the way).

It seems the error case is actually ignored, like I wrote above. This is probably a bug in itself (or maybe not, I'll have to check the intent with the original authors, I guess).

@flip1995
Copy link
Member

I'll have to check the intent with the original authors

If it should be a bug of this lint, feel free to open an issue about it 👍

@mleduque
Copy link

I don't think I'm qualified enough to decide if there is a bug or not :D

I also found some
let _ = SHARED_OBJECT.lock().map_or_else(|error| {...}, |content| { ... })

So I think some of the map ones may actually be as intended (but possibly not all of them, but I haven't yet had the authors input on the subject).

@smoelius smoelius mentioned this pull request Nov 8, 2020
bors added a commit that referenced this pull request Nov 9, 2020
Add `let_underscore_drop`

This line generalizes `let_underscore_lock` (#5101) to warn about any initializer expression that implements `Drop`.

So, for example, the following would generate a warning:
```rust
struct Droppable;
impl Drop for Droppable {
    fn drop(&mut self) {}
}
let _ = Droppable;
```

I tried to preserve the original `let_underscore_lock` functionality in the sense that the warning generated for
```rust
let _ = mutex.lock();
```
should be unchanged.

*Please keep the line below*
changelog: Add lint [`let_underscore_drop`]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: The marked PR was approved and is only waiting bors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn on let _ = x.lock();
6 participants