Skip to content

Commit d4c535b

Browse files
committed
fix: nonminimal_bool wrongly showed the macro definition
1 parent 4800557 commit d4c535b

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

clippy_lints/src/booleans.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,14 @@ impl SuggestContext<'_, '_, '_> {
350350
self.output.push_str(&str);
351351
} else {
352352
self.output.push('!');
353-
self.output
354-
.push_str(&Sugg::hir_opt(self.cx, terminal)?.maybe_par().to_string());
353+
if terminal.span.from_expansion()
354+
&& let Some(orig) = terminal.span.source_callsite().get_source_text(self.cx)
355+
{
356+
self.output.push_str(&orig);
357+
} else {
358+
self.output
359+
.push_str(&Sugg::hir_opt(self.cx, terminal)?.maybe_par().to_string());
360+
}
355361
}
356362
},
357363
True | False | Not(_) => {

clippy_utils/src/sugg.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ impl<'a> Sugg<'a> {
5353
/// Prepare a suggestion from an expression.
5454
pub fn hir_opt(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<Self> {
5555
let ctxt = expr.span.ctxt();
56-
let get_snippet = |span| snippet_with_context(cx, span, ctxt, "", &mut Applicability::Unspecified).0;
56+
let get_snippet =
57+
|span: Span| snippet_with_context(cx, span.source_callsite(), ctxt, "", &mut Applicability::Unspecified).0;
5758
snippet_opt(cx, expr.span).map(|_| Self::hir_from_snippet(expr, get_snippet))
5859
}
5960

tests/ui/nonminimal_bool.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,23 @@ fn issue14184(a: f32, b: bool) {
216216
println!("Hi");
217217
}
218218
}
219+
220+
mod issue14404 {
221+
enum TyKind {
222+
Ref(i32, i32, i32),
223+
Other,
224+
}
225+
226+
struct Expr;
227+
228+
fn is_mutable(expr: &Expr) -> bool {
229+
todo!()
230+
}
231+
232+
fn should_not_give_macro(ty: TyKind, expr: Expr) {
233+
if !(matches!(ty, TyKind::Ref(_, _, _)) && !is_mutable(&expr)) {
234+
//~^ nonminimal_bool
235+
todo!()
236+
}
237+
}
238+
}

tests/ui/nonminimal_bool.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,11 @@ error: this boolean expression can be simplified
229229
LL | if !(a < 2.0 && !b) {
230230
| ^^^^^^^^^^^^^^^^ help: try: `!(a < 2.0) || b`
231231

232-
error: aborting due to 30 previous errors
232+
error: this boolean expression can be simplified
233+
--> tests/ui/nonminimal_bool.rs:233:12
234+
|
235+
LL | if !(matches!(ty, TyKind::Ref(_, _, _)) && !is_mutable(&expr)) {
236+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `!matches!(ty, TyKind::Ref(_, _, _)) || is_mutable(&expr)`
237+
238+
error: aborting due to 31 previous errors
233239

0 commit comments

Comments
 (0)