-
Notifications
You must be signed in to change notification settings - Fork 1.6k
suspicious_map oddity #5253
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
Comments
I guess we should bail out, if some expr in the |
Similar issue is not only with assignment but also a function, like fn count_and_sum(numbers: &Vec<u32>) -> (usize, u32) {
let mut sum: u32 = 0;
let mut sum_mut = |n| sum += n;
let count = numbers.iter().map(|n| sum_mut(*n)).count();
(count, sum)
} Another case is let count = numbers.iter().filter(|n| **n > 2).map(|n| sum +n).count(); Overall, I do like the current lint message, calling |
Can |
I'm going to take a stab at it. |
I think I've made progress. Writing a test now. |
Meh. Brain fart. |
If the closure in the `map` call ends up mutating a variable, the call is assumed to no longer be suspicious. Given just a function name or path, there's no way to detect that there is interiour mutability, so the lint still fires in that case. However, it is now documented as a known problem. Closes: rust-lang#5253
See #5375. Still a hole, but the "common" case of a literal closure should no longer be a false positive. |
If the closure in the `map` call ends up mutating a variable, the call is assumed to no longer be suspicious. Given just a function name or path, there's no way to detect that there is interiour mutability, so the lint still fires in that case. However, it is now documented as a known problem. Fixes rust-lang#5253
If the closure in the `map` call ends up mutating a variable, the call is assumed to no longer be suspicious. Given just a function name or path, there's no way to detect that there is interiour mutability, so the lint still fires in that case. However, it is now documented as a known problem. Fixes rust-lang#5253
If the closure in the `map` call ends up mutating a variable, the call is assumed to no longer be suspicious. Given just a function name or path, there's no way to detect that there is interiour mutability, so the lint still fires in that case. However, it is now documented as a known problem. Fixes rust-lang#5253
If the closure in the `map` call ends up mutating a variable, the call is assumed to no longer be suspicious. Given just a function name or path, there's no way to detect that there is interiour mutability, so the lint still fires in that case. However, it is now documented as a known problem. Fixes rust-lang#5253
Ran into a similar case, that undesirably triggered this lint. I essentially want to perform a My example can be boiled down to the following, which also triggers the lint: let v = vec![
String::from("foo"),
String::from("bar"),
String::from("baz"),
];
let count = v
.into_iter()
.map(|item| {
println!("{}", item);
})
.count();
println!("Processed {} items", count); In my actual code, the map closure moves the item of course. |
If the closure in the `map` call ends up mutating a variable, the call is assumed to no longer be suspicious. Given just a function name or path, there's no way to detect that there is interiour mutability, so the lint still fires in that case. However, it is now documented as a known problem. Fixes rust-lang#5253
In this example, we use
map
to process all values and do something with them that involves another mutable variable or function, in the end, we usecount()
to get the total number of items processed.The lint explanation says
But a filter is not useful here. Using
for_each
is out of option sincefor_each().count()
does not work sincefor_each()
does not return anything.I'm not sure if
inspect()
is the right choice here, at least it is not noted in the lints description.The text was updated successfully, but these errors were encountered: