revert manual_assert suggestion refactor

Because `Sugg` helper does not simplify multiple negations
This commit is contained in:
kraktus 2022-09-16 21:38:56 +02:00
parent 2c04c1a188
commit a35734c172
5 changed files with 15 additions and 12 deletions

View file

@ -4,7 +4,7 @@ use clippy_utils::macros::{root_macro_call, FormatArgsExpn};
use clippy_utils::source::snippet_with_applicability; use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{peel_blocks_with_stmt, span_extract_comment, sugg}; use clippy_utils::{peel_blocks_with_stmt, span_extract_comment, sugg};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind}; use rustc_hir::{Expr, ExprKind, UnOp};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym; use rustc_span::sym;
@ -55,9 +55,12 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
if !comments.is_empty() { if !comments.is_empty() {
comments += "\n"; comments += "\n";
} }
// we need to negate the <cond> expression because `assert!` panics when <cond> is `false`, wherease original pattern panicked when evaluating to `true` let (cond, not) = match cond.kind {
let cond_sugg = !sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability); ExprKind::Unary(UnOp::Not, e) => (e, ""),
let sugg = format!("assert!({cond_sugg}, {format_args_snip});"); _ => (cond, "!"),
};
let cond_sugg = sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par();
let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip});");
// we show to the user the suggestion without the comments, but when applicating the fix, include the comments in the block // we show to the user the suggestion without the comments, but when applicating the fix, include the comments in the block
span_lint_and_then( span_lint_and_then(
cx, cx,

View file

@ -28,8 +28,8 @@ fn main() {
{ {
panic!("qaqaq{:?}", a); panic!("qaqaq{:?}", a);
} }
assert!(!(!a.is_empty()), "qaqaq{:?}", a); assert!(a.is_empty(), "qaqaq{:?}", a);
assert!(!(!a.is_empty()), "qwqwq"); assert!(a.is_empty(), "qwqwq");
if a.len() == 3 { if a.len() == 3 {
println!("qwq"); println!("qwq");
println!("qwq"); println!("qwq");

View file

@ -9,7 +9,7 @@ LL | | }
= note: `-D clippy::manual-assert` implied by `-D warnings` = note: `-D clippy::manual-assert` implied by `-D warnings`
help: try instead help: try instead
| |
LL | assert!(!(!a.is_empty()), "qaqaq{:?}", a); LL | assert!(a.is_empty(), "qaqaq{:?}", a);
| |
error: only a `panic!` in `if`-then statement error: only a `panic!` in `if`-then statement
@ -22,7 +22,7 @@ LL | | }
| |
help: try instead help: try instead
| |
LL | assert!(!(!a.is_empty()), "qwqwq"); LL | assert!(a.is_empty(), "qwqwq");
| |
error: only a `panic!` in `if`-then statement error: only a `panic!` in `if`-then statement

View file

@ -28,8 +28,8 @@ fn main() {
{ {
panic!("qaqaq{:?}", a); panic!("qaqaq{:?}", a);
} }
assert!(!(!a.is_empty()), "qaqaq{:?}", a); assert!(a.is_empty(), "qaqaq{:?}", a);
assert!(!(!a.is_empty()), "qwqwq"); assert!(a.is_empty(), "qwqwq");
if a.len() == 3 { if a.len() == 3 {
println!("qwq"); println!("qwq");
println!("qwq"); println!("qwq");

View file

@ -9,7 +9,7 @@ LL | | }
= note: `-D clippy::manual-assert` implied by `-D warnings` = note: `-D clippy::manual-assert` implied by `-D warnings`
help: try instead help: try instead
| |
LL | assert!(!(!a.is_empty()), "qaqaq{:?}", a); LL | assert!(a.is_empty(), "qaqaq{:?}", a);
| |
error: only a `panic!` in `if`-then statement error: only a `panic!` in `if`-then statement
@ -22,7 +22,7 @@ LL | | }
| |
help: try instead help: try instead
| |
LL | assert!(!(!a.is_empty()), "qwqwq"); LL | assert!(a.is_empty(), "qwqwq");
| |
error: only a `panic!` in `if`-then statement error: only a `panic!` in `if`-then statement