-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
suspicious_map: accept mutation in map().count()
#5375
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
#![warn(clippy::suspicious_map)] | ||
#![allow(clippy::redundant_closure)] | ||
|
||
fn main() { | ||
let _ = (0..3).map(|x| x + 2).count(); | ||
|
||
// This usage is OK because the `sum` side effect makes the `map` useful. | ||
let mut sum = 0; | ||
let _ = (0..3).map(|x| sum += x).count(); | ||
|
||
// The linter is blind to path-based arguments however. | ||
let mut ext_sum = 0; | ||
let ext_closure = |x| ext_sum += x; | ||
let _ = (0..3).map(ext_closure).count(); | ||
|
||
// The linter can see `FnMut` calls however. | ||
let mut ext_closure_inner = |x| ext_sum += x; | ||
let _ = (0..3).map(|x| ext_closure_inner(x)).count(); | ||
} |
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 |
---|---|---|
@@ -1,11 +1,19 @@ | ||
error: this call to `map()` won't have an effect on the call to `count()` | ||
--> $DIR/suspicious_map.rs:4:13 | ||
--> $DIR/suspicious_map.rs:5:13 | ||
| | ||
LL | let _ = (0..3).map(|x| x + 2).count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::suspicious-map` implied by `-D warnings` | ||
= help: make sure you did not confuse `map` with `filter` or `for_each` | ||
|
||
error: aborting due to previous error | ||
error: this call to `map()` won't have an effect on the call to `count()` | ||
--> $DIR/suspicious_map.rs:14:13 | ||
| | ||
LL | let _ = (0..3).map(ext_closure).count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: make sure you did not confuse `map` with `filter` or `for_each` | ||
|
||
error: aborting due to 2 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.
Hm, this case still gets linted...
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 we get is the name of the function. Should we do name lookup and look into the implementation to check for mutated (non-local) variables?
Which makes me think…I suppose mutable local variables will false-negative this as well :/ . Hrm.
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.
Or can we do a name lookup and somehow ask if it is
FnMut
?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 was hoping, that
maybe_body_owned_by
will get theBodyId
of the closure and that it would Just Work™ withmutable_variables
. Can you check if it even does get aBodyId
?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.
It does get a
BodyId
, but doesn't detect any mutated variables in it.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.
So none of the examples in the test are
Upvar
instances when going throughmutated_variables
. I'm not sure what's going on here right now, but I suspect theCopy
-ness is confusing things here?