Skip to content

Commit dfe08fb

Browse files
committed
Include macro callsites in suggestions
1 parent df828b9 commit dfe08fb

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

clippy_lints/src/matches.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass, in_exte
1313
use crate::rustc::{declare_tool_lint, lint_array};
1414
use if_chain::if_chain;
1515
use crate::rustc::ty::{self, Ty};
16-
use std::borrow::Cow;
1716
use std::cmp::Ordering;
1817
use std::collections::Bound;
1918
use crate::syntax::ast::LitKind;
@@ -257,15 +256,17 @@ fn report_single_match_single_pattern(cx: &LateContext<'_, '_>, ex: &Expr, arms:
257256
};
258257
let els_str = els.map_or(String::new(), |els| {
259258
if in_macro(els.span) {
260-
" else { .. }".to_string()
259+
let source_callsite_snip = snippet(cx, els.span.source_callsite(), "..");
260+
format!(" else {{ {} }}", source_callsite_snip)
261261
} else {
262262
format!(" else {}", expr_block(cx, els, None, ".."))
263263
}
264264
});
265265
let expr_block = if in_macro(arms[0].body.span) {
266-
Cow::Owned("{ .. }".to_string())
266+
let source_callsite_snip = snippet(cx, arms[0].body.span.source_callsite(), "..");
267+
format!("{{ {} }}", source_callsite_snip)
267268
} else {
268-
expr_block(cx, &arms[0].body, None, "..")
269+
expr_block(cx, &arms[0].body, None, "..").to_string()
269270
};
270271
span_lint_and_sugg(
271272
cx,

tests/ui/matches.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ error: you seem to be trying to use match for destructuring a single pattern. Co
3333
51 | | &(v, 1) => println!("{}", v),
3434
52 | | _ => println!("none"),
3535
53 | | }
36-
| |_____^ help: try this: `if let &(v, 1) = tup { .. } else { .. }`
36+
| |_____^ help: try this: `if let &(v, 1) = tup { println!("{}", v) } else { println!("none") }`
3737

3838
error: you don't need to add `&` to all patterns
3939
--> $DIR/matches.rs:50:5

tests/ui/single_match.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ error: you seem to be trying to use match for destructuring a single pattern. Co
1919
31 | | Some(y) => println!("{:?}", y),
2020
32 | | _ => ()
2121
33 | | }
22-
| |_____^ help: try this: `if let Some(y) = x { .. }`
22+
| |_____^ help: try this: `if let Some(y) = x { println!("{:?}", y) }`
2323

2424
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
2525
--> $DIR/single_match.rs:36:5

0 commit comments

Comments
 (0)