Skip to content

Commit e27fe83

Browse files
committed
change after review
1 parent a495b82 commit e27fe83

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

clippy_lints/src/bool_assert_comparison.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::{diagnostics::span_lint_and_sugg, higher, is_direct_expn_of, source, ty::implements_trait};
1+
use clippy_utils::{diagnostics::span_lint_and_sugg, higher, is_direct_expn_of, sugg::Sugg, ty::implements_trait};
22
use rustc_ast::ast::LitKind;
33
use rustc_errors::Applicability;
44
use rustc_hir::{Expr, ExprKind, Lit};
@@ -80,9 +80,7 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
8080
{
8181
if let Some(span) = is_direct_expn_of(expr.span, mac) {
8282
if let Some(args) = higher::extract_assert_macro_args(expr) {
83-
if let [a, b, ..] = args[..] {
84-
//let nb_bool_args = is_bool_lit(a) as usize + is_bool_lit(b) as usize;
85-
83+
if let [a, b, ref fmt_args @ ..] = args[..] {
8684
let (lit_value, other_expr) = match (bool_lit(a), bool_lit(b)) {
8785
(Some(lit), None) => (lit, b),
8886
(None, Some(lit)) => (lit, a),
@@ -103,20 +101,38 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
103101
}
104102

105103
let non_eq_mac = &mac[..mac.len() - 3];
104+
let mut applicability = Applicability::MaybeIncorrect;
105+
let sugg = Sugg::hir_with_applicability(cx, other_expr, "..", &mut applicability);
106106
let expr_string = if lit_value ^ is_eq {
107-
format!("!({})", source::snippet(cx, other_expr.span, ""))
107+
format!("!{}", sugg.maybe_par())
108108
} else {
109-
source::snippet(cx, other_expr.span, "").to_string()
109+
format!("{}", sugg.maybe_par())
110110
};
111111

112-
let suggestion = if args.len() <= 2 {
113-
format!("{}!({})", non_eq_mac, expr_string)
112+
let arg_span = match fmt_args {
113+
[] => None,
114+
[a] => Some(format!(
115+
"{}",
116+
Sugg::hir_with_applicability(cx, a, "..", &mut applicability)
117+
)),
118+
_ => {
119+
let mut args = format!(
120+
"{}",
121+
Sugg::hir_with_applicability(cx, fmt_args[0], "..", &mut applicability)
122+
);
123+
for el in &fmt_args[1..] {
124+
args.push_str(&format!(
125+
", {}",
126+
Sugg::hir_with_applicability(cx, el, "..", &mut applicability)
127+
));
128+
}
129+
Some(args)
130+
},
131+
};
132+
let suggestion = if let Some(spans) = arg_span {
133+
format!("{}!({}, {})", non_eq_mac, expr_string, spans)
114134
} else {
115-
let mut str = format!("{}!({}", non_eq_mac, expr_string);
116-
for el in &args[2..] {
117-
str = format!("{}, {}", str, source::snippet(cx, el.span, ""));
118-
}
119-
format!("{})", str)
135+
format!("{}!({})", non_eq_mac, expr_string)
120136
};
121137
span_lint_and_sugg(
122138
cx,

tests/ui/bool_assert_comparison.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: used `assert_eq!` with a literal bool
22
--> $DIR/bool_assert_comparison.rs:69:5
33
|
44
LL | assert_eq!("a".is_empty(), false);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!("a".is_empty()))`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty())`
66
|
77
= note: `-D clippy::bool-assert-comparison` implied by `-D warnings`
88

@@ -34,25 +34,25 @@ error: used `assert_ne!` with a literal bool
3434
--> $DIR/bool_assert_comparison.rs:80:5
3535
|
3636
LL | assert_ne!("".is_empty(), true);
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!("".is_empty()))`
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"".is_empty())`
3838

3939
error: used `assert_ne!` with a literal bool
4040
--> $DIR/bool_assert_comparison.rs:81:5
4141
|
4242
LL | assert_ne!(true, "".is_empty());
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!("".is_empty()))`
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"".is_empty())`
4444

4545
error: used `assert_ne!` with a literal bool
4646
--> $DIR/bool_assert_comparison.rs:86:5
4747
|
4848
LL | assert_ne!(b, true);
49-
| ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!(b))`
49+
| ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!b)`
5050

5151
error: used `debug_assert_eq!` with a literal bool
5252
--> $DIR/bool_assert_comparison.rs:89:5
5353
|
5454
LL | debug_assert_eq!("a".is_empty(), false);
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!("a".is_empty()))`
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty())`
5656

5757
error: used `debug_assert_eq!` with a literal bool
5858
--> $DIR/bool_assert_comparison.rs:90:5
@@ -82,55 +82,55 @@ error: used `debug_assert_ne!` with a literal bool
8282
--> $DIR/bool_assert_comparison.rs:100:5
8383
|
8484
LL | debug_assert_ne!("".is_empty(), true);
85-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!("".is_empty()))`
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"".is_empty())`
8686

8787
error: used `debug_assert_ne!` with a literal bool
8888
--> $DIR/bool_assert_comparison.rs:101:5
8989
|
9090
LL | debug_assert_ne!(true, "".is_empty());
91-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!("".is_empty()))`
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"".is_empty())`
9292

9393
error: used `debug_assert_ne!` with a literal bool
9494
--> $DIR/bool_assert_comparison.rs:106:5
9595
|
9696
LL | debug_assert_ne!(b, true);
97-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!(b))`
97+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!b)`
9898

9999
error: used `assert_eq!` with a literal bool
100100
--> $DIR/bool_assert_comparison.rs:111:5
101101
|
102102
LL | assert_eq!("a".is_empty(), false, "tadam {}", 1);
103-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!("a".is_empty()))`
103+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty())`
104104

105105
error: used `assert_eq!` with a literal bool
106106
--> $DIR/bool_assert_comparison.rs:112:5
107107
|
108108
LL | assert_eq!("a".is_empty(), false, "tadam {}", true);
109-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!("a".is_empty()))`
109+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty())`
110110

111111
error: used `assert_eq!` with a literal bool
112112
--> $DIR/bool_assert_comparison.rs:113:5
113113
|
114114
LL | assert_eq!(false, "a".is_empty(), "tadam {}", true);
115-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!("a".is_empty()))`
115+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty())`
116116

117117
error: used `debug_assert_eq!` with a literal bool
118118
--> $DIR/bool_assert_comparison.rs:118:5
119119
|
120120
LL | debug_assert_eq!("a".is_empty(), false, "tadam {}", 1);
121-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!("a".is_empty()))`
121+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty())`
122122

123123
error: used `debug_assert_eq!` with a literal bool
124124
--> $DIR/bool_assert_comparison.rs:119:5
125125
|
126126
LL | debug_assert_eq!("a".is_empty(), false, "tadam {}", true);
127-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!("a".is_empty()))`
127+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty())`
128128

129129
error: used `debug_assert_eq!` with a literal bool
130130
--> $DIR/bool_assert_comparison.rs:120:5
131131
|
132132
LL | debug_assert_eq!(false, "a".is_empty(), "tadam {}", true);
133-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!("a".is_empty()))`
133+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty())`
134134

135135
error: aborting due to 22 previous errors
136136

0 commit comments

Comments
 (0)