Skip to content

Commit c199d90

Browse files
author
Michael Wright
committed
Fix match_same_arms false negative
Closes #4244
1 parent cc8b221 commit c199d90

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

clippy_lints/src/copies.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::utils::{get_parent_expr, higher, in_macro_or_desugar, snippet, span_lint_and_then, span_note_and_lint};
1+
use crate::utils::{
2+
get_parent_expr, higher, in_macro_or_desugar, same_tys, snippet, span_lint_and_then, span_note_and_lint,
3+
};
24
use crate::utils::{SpanlessEq, SpanlessHash};
35
use rustc::hir::*;
46
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -165,7 +167,18 @@ fn lint_same_cond(cx: &LateContext<'_, '_>, conds: &[&Expr]) {
165167
}
166168

167169
/// Implementation of `MATCH_SAME_ARMS`.
168-
fn lint_match_arms(cx: &LateContext<'_, '_>, expr: &Expr) {
170+
fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr) {
171+
fn same_bindings<'tcx>(
172+
cx: &LateContext<'_, 'tcx>,
173+
lhs: &FxHashMap<LocalInternedString, Ty<'tcx>>,
174+
rhs: &FxHashMap<LocalInternedString, Ty<'tcx>>,
175+
) -> bool {
176+
lhs.len() == rhs.len()
177+
&& lhs
178+
.iter()
179+
.all(|(name, l_ty)| rhs.get(name).map_or(false, |r_ty| same_tys(cx, l_ty, r_ty)))
180+
}
181+
169182
if let ExprKind::Match(_, ref arms, MatchSource::Normal) = expr.node {
170183
let hash = |&(_, arm): &(usize, &Arm)| -> u64 {
171184
let mut h = SpanlessHash::new(cx, cx.tables);
@@ -176,12 +189,13 @@ fn lint_match_arms(cx: &LateContext<'_, '_>, expr: &Expr) {
176189
let eq = |&(lindex, lhs): &(usize, &Arm), &(rindex, rhs): &(usize, &Arm)| -> bool {
177190
let min_index = usize::min(lindex, rindex);
178191
let max_index = usize::max(lindex, rindex);
192+
179193
// Arms with a guard are ignored, those can’t always be merged together
180194
// This is also the case for arms in-between each there is an arm with a guard
181195
(min_index..=max_index).all(|index| arms[index].guard.is_none()) &&
182196
SpanlessEq::new(cx).eq_expr(&lhs.body, &rhs.body) &&
183197
// all patterns should have the same bindings
184-
bindings(cx, &lhs.pats[0]) == bindings(cx, &rhs.pats[0])
198+
same_bindings(cx, &bindings(cx, &lhs.pats[0]), &bindings(cx, &rhs.pats[0]))
185199
};
186200

187201
let indexed_arms: Vec<(usize, &Arm)> = arms.iter().enumerate().collect();

tests/ui/match_same_arms.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![warn(clippy::match_same_arms)]
12
#![allow(
23
clippy::blacklisted_name,
34
clippy::collapsible_if,
@@ -21,7 +22,6 @@ pub enum Abc {
2122
C,
2223
}
2324

24-
#[warn(clippy::match_same_arms)]
2525
#[allow(clippy::unused_unit)]
2626
fn match_same_arms() {
2727
let _ = match 42 {
@@ -126,4 +126,21 @@ fn match_same_arms() {
126126
};
127127
}
128128

129+
mod issue4244 {
130+
#[derive(PartialEq, PartialOrd, Eq, Ord)]
131+
pub enum CommandInfo {
132+
BuiltIn { name: String, about: Option<String> },
133+
External { name: String, path: std::path::PathBuf },
134+
}
135+
136+
impl CommandInfo {
137+
pub fn name(&self) -> String {
138+
match self {
139+
CommandInfo::BuiltIn { name, .. } => name.to_string(),
140+
CommandInfo::External { name, .. } => name.to_string(),
141+
}
142+
}
143+
}
144+
}
145+
129146
fn main() {}

tests/ui/match_same_arms.stderr

+18-1
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,22 @@ help: consider refactoring into `2 | 3`
224224
LL | 2 => 2, //~ ERROR 2nd matched arms have same body
225225
| ^
226226

227-
error: aborting due to 12 previous errors
227+
error: this `match` has identical arm bodies
228+
--> $DIR/match_same_arms.rs:140:55
229+
|
230+
LL | CommandInfo::External { name, .. } => name.to_string(),
231+
| ^^^^^^^^^^^^^^^^
232+
|
233+
note: same as this
234+
--> $DIR/match_same_arms.rs:139:54
235+
|
236+
LL | CommandInfo::BuiltIn { name, .. } => name.to_string(),
237+
| ^^^^^^^^^^^^^^^^
238+
help: consider refactoring into `CommandInfo::BuiltIn { name, .. } | CommandInfo::External { name, .. }`
239+
--> $DIR/match_same_arms.rs:139:17
240+
|
241+
LL | CommandInfo::BuiltIn { name, .. } => name.to_string(),
242+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
243+
244+
error: aborting due to 13 previous errors
228245

0 commit comments

Comments
 (0)