mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-02 19:53:46 +00:00
Use utils::sugg
in MATCH_BOOL
This commit is contained in:
parent
66808c1e77
commit
7a1fc9fce5
@ -10,6 +10,7 @@ use syntax::ast::LitKind;
|
|||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use utils::paths;
|
use utils::paths;
|
||||||
use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block};
|
use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block};
|
||||||
|
use utils::sugg::Sugg;
|
||||||
|
|
||||||
/// **What it does:** This lint checks for matches with a single arm where an `if let` will usually suffice.
|
/// **What it does:** This lint checks for matches with a single arm where an `if let` will usually suffice.
|
||||||
///
|
///
|
||||||
@ -262,8 +263,9 @@ fn check_match_bool(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
|
|||||||
Some(format!("if {} {}", snippet(cx, ex.span, "b"), expr_block(cx, true_expr, None, "..")))
|
Some(format!("if {} {}", snippet(cx, ex.span, "b"), expr_block(cx, true_expr, None, "..")))
|
||||||
}
|
}
|
||||||
(true, false) => {
|
(true, false) => {
|
||||||
Some(format!("try\nif !{} {}",
|
let test = &Sugg::hir(cx, ex, "..");
|
||||||
snippet(cx, ex.span, "b"),
|
Some(format!("if {} {}",
|
||||||
|
!test,
|
||||||
expr_block(cx, false_expr, None, "..")))
|
expr_block(cx, false_expr, None, "..")))
|
||||||
}
|
}
|
||||||
(true, true) => None,
|
(true, true) => None,
|
||||||
|
@ -123,6 +123,13 @@ impl<'a, 'b> std::ops::Sub<&'b Sugg<'b>> for &'a Sugg<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> std::ops::Not for &'a Sugg<'a> {
|
||||||
|
type Output = Sugg<'static>;
|
||||||
|
fn not(self) -> Sugg<'static> {
|
||||||
|
make_unop("!", self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct ParenHelper<T> {
|
struct ParenHelper<T> {
|
||||||
paren: bool,
|
paren: bool,
|
||||||
wrapped: T,
|
wrapped: T,
|
||||||
@ -147,6 +154,15 @@ impl<T: std::fmt::Display> std::fmt::Display for ParenHelper<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Build the string for `<op> <expr>` adding parenthesis when necessary.
|
||||||
|
///
|
||||||
|
/// For convenience, the operator is taken as a string because all unary operators have the same
|
||||||
|
/// precedence.
|
||||||
|
pub fn make_unop(op: &str, expr: &Sugg) -> Sugg<'static> {
|
||||||
|
let needs_paren = !matches!(*expr, Sugg::NonParen(..));
|
||||||
|
Sugg::MaybeParen(format!("{}{}", op, ParenHelper::new(needs_paren, expr)).into())
|
||||||
|
}
|
||||||
|
|
||||||
/// Build the string for `<lhs> <op> <rhs>` adding parenthesis when necessary.
|
/// Build the string for `<lhs> <op> <rhs>` adding parenthesis when necessary.
|
||||||
///
|
///
|
||||||
/// Precedence of shift operator relative to other arithmetic operation is often confusing so
|
/// Precedence of shift operator relative to other arithmetic operation is often confusing so
|
||||||
|
@ -130,7 +130,7 @@ fn match_bool() {
|
|||||||
match test {
|
match test {
|
||||||
//~^ ERROR you seem to be trying to match on a boolean expression
|
//~^ ERROR you seem to be trying to match on a boolean expression
|
||||||
//~| HELP try
|
//~| HELP try
|
||||||
//~^^ SUGGESTION if !test { println!("Noooo!"); };
|
//~| SUGGESTION if !test { println!("Noooo!"); };
|
||||||
true => (),
|
true => (),
|
||||||
false => { println!("Noooo!"); }
|
false => { println!("Noooo!"); }
|
||||||
};
|
};
|
||||||
@ -138,7 +138,16 @@ fn match_bool() {
|
|||||||
match test {
|
match test {
|
||||||
//~^ ERROR you seem to be trying to match on a boolean expression
|
//~^ ERROR you seem to be trying to match on a boolean expression
|
||||||
//~| HELP try
|
//~| HELP try
|
||||||
//~^^ SUGGESTION if !test { println!("Noooo!"); };
|
//~| SUGGESTION if !test { println!("Noooo!"); };
|
||||||
|
false => { println!("Noooo!"); }
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
|
|
||||||
|
match test && test {
|
||||||
|
//~^ ERROR you seem to be trying to match on a boolean expression
|
||||||
|
//~| HELP try
|
||||||
|
//~| SUGGESTION if !(test && test) { println!("Noooo!"); };
|
||||||
|
//~| ERROR equal expressions as operands
|
||||||
false => { println!("Noooo!"); }
|
false => { println!("Noooo!"); }
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user