Fix match_same_arms to fail late
Changes: - Add a function search_same_list which return a list of matched expressions - Change the match_same_arms implementation behaviour. It will lint each same arms found.
This commit is contained in:
parent
f49d878ce5
commit
902726c38d
@ -185,44 +185,48 @@ fn lint_match_arms(cx: &LateContext<'_, '_>, expr: &Expr) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let indexed_arms: Vec<(usize, &Arm)> = arms.iter().enumerate().collect();
|
let indexed_arms: Vec<(usize, &Arm)> = arms.iter().enumerate().collect();
|
||||||
if let Some((&(_, i), &(_, j))) = search_same(&indexed_arms, hash, eq) {
|
search_same_list(&indexed_arms, hash, eq).map(|item| {
|
||||||
span_lint_and_then(
|
for match_expr in item {
|
||||||
cx,
|
let (&(_, i), &(_, j)) = match_expr;
|
||||||
MATCH_SAME_ARMS,
|
|
||||||
j.body.span,
|
|
||||||
"this `match` has identical arm bodies",
|
|
||||||
|db| {
|
|
||||||
db.span_note(i.body.span, "same as this");
|
|
||||||
|
|
||||||
// Note: this does not use `span_suggestion` on purpose:
|
span_lint_and_then(
|
||||||
// there is no clean way
|
cx,
|
||||||
// to remove the other arm. Building a span and suggest to replace it to ""
|
MATCH_SAME_ARMS,
|
||||||
// makes an even more confusing error message. Also in order not to make up a
|
j.body.span,
|
||||||
// span for the whole pattern, the suggestion is only shown when there is only
|
"this `match` has identical arm bodies",
|
||||||
// one pattern. The user should know about `|` if they are already using it…
|
|db| {
|
||||||
|
db.span_note(i.body.span, "same as this");
|
||||||
|
|
||||||
if i.pats.len() == 1 && j.pats.len() == 1 {
|
// Note: this does not use `span_suggestion` on purpose:
|
||||||
let lhs = snippet(cx, i.pats[0].span, "<pat1>");
|
// there is no clean way
|
||||||
let rhs = snippet(cx, j.pats[0].span, "<pat2>");
|
// to remove the other arm. Building a span and suggest to replace it to ""
|
||||||
|
// makes an even more confusing error message. Also in order not to make up a
|
||||||
|
// span for the whole pattern, the suggestion is only shown when there is only
|
||||||
|
// one pattern. The user should know about `|` if they are already using it…
|
||||||
|
|
||||||
if let PatKind::Wild = j.pats[0].node {
|
if i.pats.len() == 1 && j.pats.len() == 1 {
|
||||||
// if the last arm is _, then i could be integrated into _
|
let lhs = snippet(cx, i.pats[0].span, "<pat1>");
|
||||||
// note that i.pats[0] cannot be _, because that would mean that we're
|
let rhs = snippet(cx, j.pats[0].span, "<pat2>");
|
||||||
// hiding all the subsequent arms, and rust won't compile
|
|
||||||
db.span_note(
|
if let PatKind::Wild = j.pats[0].node {
|
||||||
i.body.span,
|
// if the last arm is _, then i could be integrated into _
|
||||||
&format!(
|
// note that i.pats[0] cannot be _, because that would mean that we're
|
||||||
"`{}` has the same arm body as the `_` wildcard, consider removing it`",
|
// hiding all the subsequent arms, and rust won't compile
|
||||||
lhs
|
db.span_note(
|
||||||
),
|
i.body.span,
|
||||||
);
|
&format!(
|
||||||
} else {
|
"`{}` has the same arm body as the `_` wildcard, consider removing it`",
|
||||||
db.span_note(i.body.span, &format!("consider refactoring into `{} | {}`", lhs, rhs));
|
lhs
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
db.span_note(i.body.span, &format!("consider refactoring into `{} | {}`", lhs, rhs));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
);
|
||||||
);
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,3 +364,36 @@ where
|
|||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn search_same_list<T, Hash, Eq>(exprs: &[T], hash: Hash, eq: Eq) -> Option<Vec<(&T, &T)>>
|
||||||
|
where
|
||||||
|
Hash: Fn(&T) -> u64,
|
||||||
|
Eq: Fn(&T, &T) -> bool,
|
||||||
|
{
|
||||||
|
let mut match_expr_list: Vec<(&T, &T)> = Vec::new();
|
||||||
|
|
||||||
|
let mut map: FxHashMap<_, Vec<&_>> =
|
||||||
|
FxHashMap::with_capacity_and_hasher(exprs.len(), BuildHasherDefault::default());
|
||||||
|
|
||||||
|
for expr in exprs {
|
||||||
|
match map.entry(hash(expr)) {
|
||||||
|
Entry::Occupied(mut o) => {
|
||||||
|
for o in o.get() {
|
||||||
|
if eq(o, expr) {
|
||||||
|
match_expr_list.push((o, expr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
o.get_mut().push(expr);
|
||||||
|
},
|
||||||
|
Entry::Vacant(v) => {
|
||||||
|
v.insert(vec![expr]);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if match_expr_list.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(match_expr_list)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user