Skip to content

Commit 07e7414

Browse files
authored
fix: nonminimal_bool wrongly showed the macro definition (#14424)
Closes #14404 changelog: [`nonminimal_bool`]: fix macro definition wrongly showed in suggestions.
2 parents cad9083 + 282b61b commit 07e7414

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

clippy_lints/src/booleans.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
1313
use rustc_lint::{LateContext, LateLintPass, Level};
1414
use rustc_session::impl_lint_pass;
1515
use rustc_span::def_id::LocalDefId;
16-
use rustc_span::{Span, sym};
16+
use rustc_span::{Span, SyntaxContext, sym};
1717

1818
declare_clippy_lint! {
1919
/// ### What it does
@@ -349,9 +349,13 @@ impl SuggestContext<'_, '_, '_> {
349349
if let Some(str) = simplify_not(self.cx, self.msrv, terminal) {
350350
self.output.push_str(&str);
351351
} else {
352-
self.output.push('!');
353-
self.output
354-
.push_str(&Sugg::hir_opt(self.cx, terminal)?.maybe_par().to_string());
352+
let mut app = Applicability::MachineApplicable;
353+
let snip = Sugg::hir_with_context(self.cx, terminal, SyntaxContext::root(), "", &mut app);
354+
// Ignore the case If the expression is inside a macro expansion, or the default snippet is used
355+
if app != Applicability::MachineApplicable {
356+
return None;
357+
}
358+
self.output.push_str(&(!snip).to_string());
355359
}
356360
},
357361
True | False | Not(_) => {

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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,13 @@ error: this boolean expression can be simplified
227227
--> tests/ui/nonminimal_bool.rs:214:8
228228
|
229229
LL | if !(a < 2.0 && !b) {
230-
| ^^^^^^^^^^^^^^^^ help: try: `!(a < 2.0) || b`
230+
| ^^^^^^^^^^^^^^^^ 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)