Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
use rustc_lint::{LateContext, LateLintPass, Level};
use rustc_session::impl_lint_pass;
use rustc_span::def_id::LocalDefId;
use rustc_span::{Span, sym};
use rustc_span::{Span, SyntaxContext, sym};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -349,9 +349,13 @@ impl SuggestContext<'_, '_, '_> {
if let Some(str) = simplify_not(self.cx, self.msrv, terminal) {
self.output.push_str(&str);
} else {
self.output.push('!');
self.output
.push_str(&Sugg::hir_opt(self.cx, terminal)?.maybe_par().to_string());
let mut app = Applicability::MachineApplicable;
let snip = Sugg::hir_with_context(self.cx, terminal, SyntaxContext::root(), "", &mut app);
// Ignore the case If the expression is inside a macro expansion, or the default snippet is used
if app != Applicability::MachineApplicable {
return None;
}
self.output.push_str(&(!snip).to_string());
}
},
True | False | Not(_) => {
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/nonminimal_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,23 @@ fn issue14184(a: f32, b: bool) {
println!("Hi");
}
}

mod issue14404 {
enum TyKind {
Ref(i32, i32, i32),
Other,
}

struct Expr;

fn is_mutable(expr: &Expr) -> bool {
todo!()
}

fn should_not_give_macro(ty: TyKind, expr: Expr) {
if !(matches!(ty, TyKind::Ref(_, _, _)) && !is_mutable(&expr)) {
//~^ nonminimal_bool
todo!()
}
}
}
10 changes: 8 additions & 2 deletions tests/ui/nonminimal_bool.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,13 @@ error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool.rs:214:8
|
LL | if !(a < 2.0 && !b) {
| ^^^^^^^^^^^^^^^^ help: try: `!(a < 2.0) || b`
| ^^^^^^^^^^^^^^^^ help: try: `a >= 2.0 || b`

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

error: aborting due to 31 previous errors