Remove false positives from get_unwrap lint

HashMap and BTreeMap don't implement `IndexMut`, so we shouldn't lint
for use of `get_mut().unwrap()` for those types.
This commit is contained in:
Devon Hollowood 2016-11-01 17:48:32 -07:00
parent 4e0d182d1d
commit 6a14dc7fd4
2 changed files with 7 additions and 10 deletions

View File

@ -854,6 +854,8 @@ fn lint_iter_nth(cx: &LateContext, expr: &hir::Expr, iter_args: &MethodArgs, is_
}
fn lint_get_unwrap(cx: &LateContext, expr: &hir::Expr, get_args: &MethodArgs, is_mut: bool) {
// Note: we don't want to lint `get_mut().unwrap` for HashMap or BTreeMap,
// because they do not implement `IndexMut`
let expr_ty = cx.tcx.expr_ty(&get_args[0]);
let caller_type = if derefs_to_slice(cx, &get_args[0], expr_ty).is_some() {
"slice"
@ -861,9 +863,9 @@ fn lint_get_unwrap(cx: &LateContext, expr: &hir::Expr, get_args: &MethodArgs, is
"Vec"
} else if match_type(cx, expr_ty, &paths::VEC_DEQUE) {
"VecDeque"
} else if match_type(cx, expr_ty, &paths::HASHMAP) {
} else if !is_mut && match_type(cx, expr_ty, &paths::HASHMAP) {
"HashMap"
} else if match_type(cx, expr_ty, &paths::BTREEMAP) {
} else if !is_mut && match_type(cx, expr_ty, &paths::BTREEMAP) {
"BTreeMap"
} else {
return; // caller is not a type that we want to lint

View File

@ -445,15 +445,10 @@ fn get_unwrap() {
//~^ERROR called `.get_mut().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION &mut some_vecdeque[0]
*some_hashmap.get_mut(&1).unwrap() = 'b';
//~^ERROR called `.get_mut().unwrap()` on a HashMap. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION &mut some_hashmap[&1]
*some_btreemap.get_mut(&1).unwrap() = 'b';
//~^ERROR called `.get_mut().unwrap()` on a BTreeMap. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION &mut some_btreemap[&1]
// Check false positives
*some_hashmap.get_mut(&1).unwrap() = 'b';
*some_btreemap.get_mut(&1).unwrap() = 'b';
*false_positive.get_mut(0).unwrap() = 1;
}
}