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

Fix .map(..).unwrap_or_else(..) bad suggestion #4164

Merged
merged 2 commits into from Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use syntax::symbol::LocalInternedString;

use crate::utils::paths;
use crate::utils::sugg;
use crate::utils::usage::mutated_variables;
use crate::utils::{
get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy,
is_ctor_function, is_expn_of, is_self, is_self_ty, iter_input_pats, last_path_segment, match_def_path, match_path,
Expand Down Expand Up @@ -1880,7 +1881,20 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
// lint if the caller of `map()` is an `Option`
let is_option = match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION);
let is_result = match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::RESULT);

if is_option || is_result {
// Don't make a suggestion that may fail to compile due to mutably borrowing
// the same variable twice.
let map_mutated_vars = mutated_variables(&map_args[0], cx);
let unwrap_mutated_vars = mutated_variables(&unwrap_args[1], cx);
if let (Some(map_mutated_vars), Some(unwrap_mutated_vars)) = (map_mutated_vars, unwrap_mutated_vars) {
if map_mutated_vars.intersection(&unwrap_mutated_vars).next().is_some() {
return;
}
} else {
return;
}

// lint message
let msg = if is_option {
"called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling \
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ fn option_methods() {
// Macro case.
// Should not lint.
let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);

// Issue #4144
{
let mut frequencies = HashMap::new();
let word = "foo";

frequencies
.get_mut(word)
.map(|count| {
*count += 1;
})
.unwrap_or_else(|| {
frequencies.insert(word.to_owned(), 1);
});
}
}

/// Checks implementation of `FILTER_NEXT` lint.
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/methods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ LL | | );
| |_________________^

error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
--> $DIR/methods.rs:214:13
--> $DIR/methods.rs:229:13
|
LL | let _ = v.iter().filter(|&x| *x < 0).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -139,7 +139,7 @@ LL | let _ = v.iter().filter(|&x| *x < 0).next();
= note: replace `filter(|&x| *x < 0).next()` with `find(|&x| *x < 0)`

error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
--> $DIR/methods.rs:217:13
--> $DIR/methods.rs:232:13
|
LL | let _ = v.iter().filter(|&x| {
| _____________^
Expand All @@ -149,7 +149,7 @@ LL | | ).next();
| |___________________________^

error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:233:13
--> $DIR/methods.rs:248:13
|
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -158,7 +158,7 @@ LL | let _ = v.iter().find(|&x| *x < 0).is_some();
= note: replace `find(|&x| *x < 0).is_some()` with `any(|x| *x < 0)`

error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:236:13
--> $DIR/methods.rs:251:13
|
LL | let _ = v.iter().find(|&x| {
| _____________^
Expand All @@ -168,15 +168,15 @@ LL | | ).is_some();
| |______________________________^

error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:242:13
--> $DIR/methods.rs:257:13
|
LL | let _ = v.iter().position(|&x| x < 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `position(|&x| x < 0).is_some()` with `any(|&x| x < 0)`

error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:245:13
--> $DIR/methods.rs:260:13
|
LL | let _ = v.iter().position(|&x| {
| _____________^
Expand All @@ -186,15 +186,15 @@ LL | | ).is_some();
| |______________________________^

error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:251:13
--> $DIR/methods.rs:266:13
|
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `rposition(|&x| x < 0).is_some()` with `any(|&x| x < 0)`

error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:254:13
--> $DIR/methods.rs:269:13
|
LL | let _ = v.iter().rposition(|&x| {
| _____________^
Expand All @@ -204,7 +204,7 @@ LL | | ).is_some();
| |______________________________^

error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
--> $DIR/methods.rs:269:13
--> $DIR/methods.rs:284:13
|
LL | let _ = opt.unwrap();
| ^^^^^^^^^^^^
Expand Down