Skip to content

Commit 066f105

Browse files
committed
Auto merge of rust-lang#5992 - giraffate:fix_wrong_seggestion_in_collapsible_if, r=yaahc
Fix the wrong suggestion when using macro in `collapsible_if` Fix rust-lang#5962 changelog: Fix the wrong suggestion when using macro in `collapsible_if`
2 parents 8334a58 + 001f9e4 commit 066f105

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

clippy_lints/src/utils/sugg.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ impl<'a> Sugg<'a> {
132132
pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self {
133133
use rustc_ast::ast::RangeLimits;
134134

135-
let snippet = snippet(cx, expr.span, default);
135+
let snippet = if expr.span.from_expansion() {
136+
snippet_with_macro_callsite(cx, expr.span, default)
137+
} else {
138+
snippet(cx, expr.span, default)
139+
};
136140

137141
match expr.kind {
138142
ast::ExprKind::AddrOf(..)

tests/ui/collapsible_if.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,7 @@ fn main() {
135135
if truth() {}
136136
}
137137
}
138+
139+
// Fix #5962
140+
if matches!(true, true) && matches!(true, true) {}
138141
}

tests/ui/collapsible_if.rs

+5
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,9 @@ fn main() {
149149
if truth() {}
150150
}
151151
}
152+
153+
// Fix #5962
154+
if matches!(true, true) {
155+
if matches!(true, true) {}
156+
}
152157
}

tests/ui/collapsible_if.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,13 @@ LL | println!("Hello world!");
118118
LL | }
119119
|
120120

121-
error: aborting due to 7 previous errors
121+
error: this `if` statement can be collapsed
122+
--> $DIR/collapsible_if.rs:154:5
123+
|
124+
LL | / if matches!(true, true) {
125+
LL | | if matches!(true, true) {}
126+
LL | | }
127+
| |_____^ help: collapse nested if block: `if matches!(true, true) && matches!(true, true) {}`
128+
129+
error: aborting due to 8 previous errors
122130

0 commit comments

Comments
 (0)