Only print out question_mark lint when it actually triggered

This commit is contained in:
Peter Fürstenau 2018-12-19 20:55:01 +01:00
parent 18584698ee
commit 65c35333a4

View file

@ -73,7 +73,7 @@ impl Pass {
then { then {
let receiver_str = &Sugg::hir(cx, subject, ".."); let receiver_str = &Sugg::hir(cx, subject, "..");
let mut replacement_str = String::new(); let mut replacement: Option<String> = None;
if let Some(else_) = else_ { if let Some(else_) = else_ {
if_chain! { if_chain! {
if let ExprKind::Block(block, None) = &else_.node; if let ExprKind::Block(block, None) = &else_.node;
@ -81,28 +81,31 @@ impl Pass {
if let Some(block_expr) = &block.expr; if let Some(block_expr) = &block.expr;
if SpanlessEq::new(cx).ignore_fn().eq_expr(subject, block_expr); if SpanlessEq::new(cx).ignore_fn().eq_expr(subject, block_expr);
then { then {
replacement_str = format!("Some({}?)", receiver_str); replacement = Some(format!("Some({}?)", receiver_str));
} }
} }
} else if Self::moves_by_default(cx, subject) { } else if Self::moves_by_default(cx, subject) {
replacement_str = format!("{}.as_ref()?;", receiver_str); replacement = Some(format!("{}.as_ref()?;", receiver_str));
} else { } else {
replacement_str = format!("{}?;", receiver_str); replacement = Some(format!("{}?;", receiver_str));
} }
span_lint_and_then(
cx, if let Some(replacement_str) = replacement {
QUESTION_MARK, span_lint_and_then(
expr.span, cx,
"this block may be rewritten with the `?` operator", QUESTION_MARK,
|db| { expr.span,
db.span_suggestion_with_applicability( "this block may be rewritten with the `?` operator",
expr.span, |db| {
"replace_it_with", db.span_suggestion_with_applicability(
replacement_str, expr.span,
Applicability::MaybeIncorrect, // snippet "replace_it_with",
); replacement_str,
} Applicability::MaybeIncorrect, // snippet
) );
}
)
}
} }
} }
} }