Skip to content

Commit 30a37ef

Browse files
authored
Merge pull request #2433 from kimsnj/matches_sugg
Fix suggestions for ref matches
2 parents 7fddc61 + 21f606b commit 30a37ef

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed

clippy_lints/src/matches.rs

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use syntax::ast::LitKind;
1111
use syntax::ast::NodeId;
1212
use syntax::codemap::Span;
1313
use utils::paths;
14-
use utils::{expr_block, in_external_macro, is_allowed, is_expn_of, match_qpath, match_type, remove_blocks,
15-
snippet, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty};
14+
use utils::{expr_block, in_external_macro, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg,
15+
remove_blocks, snippet, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty};
1616
use utils::sugg::Sugg;
1717

1818
/// **What it does:** Checks for matches with a single arm where an `if let`
@@ -195,8 +195,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchPass {
195195
check_wild_err_arm(cx, ex, arms);
196196
check_match_as_ref(cx, ex, arms, expr);
197197
}
198-
if let ExprMatch(ref ex, ref arms, source) = expr.node {
199-
check_match_ref_pats(cx, ex, arms, source, expr);
198+
if let ExprMatch(ref ex, ref arms, _) = expr.node {
199+
check_match_ref_pats(cx, ex, arms, expr);
200200
}
201201
}
202202
}
@@ -400,37 +400,34 @@ fn is_panic_block(block: &Block) -> bool {
400400
}
401401
}
402402

403-
fn check_match_ref_pats(cx: &LateContext, ex: &Expr, arms: &[Arm], source: MatchSource, expr: &Expr) {
403+
fn check_match_ref_pats(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
404404
if has_only_ref_pats(arms) {
405-
if let ExprAddrOf(Mutability::MutImmutable, ref inner) = ex.node {
406-
span_lint_and_then(
407-
cx,
408-
MATCH_REF_PATS,
409-
expr.span,
405+
let mut suggs = Vec::new();
406+
let (title, msg) = if let ExprAddrOf(Mutability::MutImmutable, ref inner) = ex.node {
407+
suggs.push((ex.span, Sugg::hir(cx, inner, "..").to_string()));
408+
(
410409
"you don't need to add `&` to both the expression and the patterns",
411-
|db| {
412-
let inner = Sugg::hir(cx, inner, "..");
413-
let template = match_template(expr.span, source, &inner);
414-
db.span_suggestion(expr.span, "try", template);
415-
},
416-
);
410+
"try",
411+
)
417412
} else {
418-
span_lint_and_then(
419-
cx,
420-
MATCH_REF_PATS,
421-
expr.span,
413+
suggs.push((ex.span, Sugg::hir(cx, ex, "..").deref().to_string()));
414+
(
422415
"you don't need to add `&` to all patterns",
423-
|db| {
424-
let ex = Sugg::hir(cx, ex, "..");
425-
let template = match_template(expr.span, source, &ex.deref());
426-
db.span_suggestion(
427-
expr.span,
428-
"instead of prefixing all patterns with `&`, you can dereference the expression",
429-
template,
430-
);
431-
},
432-
);
433-
}
416+
"instead of prefixing all patterns with `&`, you can dereference the expression",
417+
)
418+
};
419+
420+
suggs.extend(arms.iter().flat_map(|a| &a.pats).filter_map(|p| {
421+
if let PatKind::Ref(ref refp, _) = p.node {
422+
Some((p.span, snippet(cx, refp.span, "..").to_string()))
423+
} else {
424+
None
425+
}
426+
}));
427+
428+
span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |db| {
429+
multispan_sugg(db, msg.to_owned(), suggs);
430+
});
434431
}
435432
}
436433

@@ -615,16 +612,6 @@ fn has_only_ref_pats(arms: &[Arm]) -> bool {
615612
mapped.map_or(false, |v| v.iter().any(|el| *el))
616613
}
617614

618-
fn match_template(span: Span, source: MatchSource, expr: &Sugg) -> String {
619-
match source {
620-
MatchSource::Normal => format!("match {} {{ .. }}", expr),
621-
MatchSource::IfLetDesugar { .. } => format!("if let .. = {} {{ .. }}", expr),
622-
MatchSource::WhileLetDesugar => format!("while let .. = {} {{ .. }}", expr),
623-
MatchSource::ForLoopDesugar => span_bug!(span, "for loop desugared to match with &-patterns!"),
624-
MatchSource::TryDesugar => span_bug!(span, "`?` operator desugared to match with &-patterns!"),
625-
}
626-
}
627-
628615
pub fn overlapping<T>(ranges: &[SpannedRange<T>]) -> Option<(&SpannedRange<T>, &SpannedRange<T>)>
629616
where
630617
T: Copy + Ord,

tests/ui/matches.stderr

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ error: you don't need to add `&` to all patterns
132132
= note: `-D match-ref-pats` implied by `-D warnings`
133133
help: instead of prefixing all patterns with `&`, you can dereference the expression
134134
|
135-
138 | match *v { .. }
135+
138 | match *v {
136+
139 | Some(v) => println!("{:?}", v),
137+
140 | None => println!("none"),
136138
|
137139

138140
error: you don't need to add `&` to all patterns
@@ -145,7 +147,8 @@ error: you don't need to add `&` to all patterns
145147
| |_____^
146148
help: instead of prefixing all patterns with `&`, you can dereference the expression
147149
|
148-
148 | match *tup { .. }
150+
148 | match *tup {
151+
149 | (v, 1) => println!("{}", v),
149152
|
150153

151154
error: you don't need to add `&` to both the expression and the patterns
@@ -155,7 +158,13 @@ error: you don't need to add `&` to both the expression and the patterns
155158
155 | | &Some(v) => println!("{:?}", v),
156159
156 | | &None => println!("none"),
157160
157 | | }
158-
| |_____^ help: try: `match w { .. }`
161+
| |_____^
162+
help: try
163+
|
164+
154 | match w {
165+
155 | Some(v) => println!("{:?}", v),
166+
156 | None => println!("none"),
167+
|
159168

160169
error: you don't need to add `&` to all patterns
161170
--> $DIR/matches.rs:165:5
@@ -166,7 +175,7 @@ error: you don't need to add `&` to all patterns
166175
| |_____^
167176
help: instead of prefixing all patterns with `&`, you can dereference the expression
168177
|
169-
165 | if let .. = *a { .. }
178+
165 | if let None = *a {
170179
|
171180

172181
error: you don't need to add `&` to both the expression and the patterns
@@ -175,7 +184,11 @@ error: you don't need to add `&` to both the expression and the patterns
175184
170 | / if let &None = &b {
176185
171 | | println!("none");
177186
172 | | }
178-
| |_____^ help: try: `if let .. = b { .. }`
187+
| |_____^
188+
help: try
189+
|
190+
170 | if let None = b {
191+
|
179192

180193
error: some ranges overlap
181194
--> $DIR/matches.rs:179:9

0 commit comments

Comments
 (0)