From 89cdd26e71ecc7778f05b06c8878d641a258519a Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Wed, 25 Sep 2019 06:20:40 +0200 Subject: [PATCH] Refactor `booleans` Inline `snip (..)` function --- clippy_lints/src/booleans.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index a85b77475e5..dafa34dec1d 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -183,7 +183,7 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> { self.output.push_str(&str) } else { self.output.push('!'); - let snip = snip(self.cx, terminal)?; + let snip = snippet_opt(self.cx, terminal.span)?; self.output.push_str(&snip); } }, @@ -215,7 +215,7 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> { } }, &Term(n) => { - let snip = snip(self.cx, self.terminals[n as usize])?; + let snip = snippet_opt(self.cx, self.terminals[n as usize].span)?; self.output.push_str(&snip); }, } @@ -223,10 +223,6 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> { } } -fn snip(cx: &LateContext<'_, '_>, e: &Expr) -> Option { - snippet_opt(cx, e.span) -} - fn simplify_not(cx: &LateContext<'_, '_>, expr: &Expr) -> Option { match &expr.node { ExprKind::Binary(binop, lhs, rhs) => { @@ -243,7 +239,14 @@ fn simplify_not(cx: &LateContext<'_, '_>, expr: &Expr) -> Option { BinOpKind::Ge => Some(" < "), _ => None, } - .and_then(|op| Some(format!("{}{}{}", snip(cx, lhs)?, op, snip(cx, rhs)?))) + .and_then(|op| { + Some(format!( + "{}{}{}", + snippet_opt(cx, lhs.span)?, + op, + snippet_opt(cx, rhs.span)? + )) + }) }, ExprKind::MethodCall(path, _, args) if args.len() == 1 => { let type_of_receiver = cx.tables.expr_ty(&args[0]); @@ -255,7 +258,7 @@ fn simplify_not(cx: &LateContext<'_, '_>, expr: &Expr) -> Option { .cloned() .flat_map(|(a, b)| vec![(a, b), (b, a)]) .find(|&(a, _)| a == path.ident.name.as_str()) - .and_then(|(_, neg_method)| Some(format!("{}.{}()", snip(cx, &args[0])?, neg_method))) + .and_then(|(_, neg_method)| Some(format!("{}.{}()", snippet_opt(cx, args[0].span)?, neg_method))) }, _ => None, }