Skip to content

Commit 65c3533

Browse files
committed
Only print out question_mark lint when it actually triggered
1 parent 1858469 commit 65c3533

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,36 +73,39 @@ impl Pass {
7373

7474
then {
7575
let receiver_str = &Sugg::hir(cx, subject, "..");
76-
let mut replacement_str = String::new();
76+
let mut replacement: Option<String> = None;
7777
if let Some(else_) = else_ {
7878
if_chain! {
7979
if let ExprKind::Block(block, None) = &else_.node;
8080
if block.stmts.len() == 0;
8181
if let Some(block_expr) = &block.expr;
8282
if SpanlessEq::new(cx).ignore_fn().eq_expr(subject, block_expr);
8383
then {
84-
replacement_str = format!("Some({}?)", receiver_str);
84+
replacement = Some(format!("Some({}?)", receiver_str));
8585
}
8686
}
8787
} else if Self::moves_by_default(cx, subject) {
88-
replacement_str = format!("{}.as_ref()?;", receiver_str);
88+
replacement = Some(format!("{}.as_ref()?;", receiver_str));
8989
} else {
90-
replacement_str = format!("{}?;", receiver_str);
90+
replacement = Some(format!("{}?;", receiver_str));
9191
}
92-
span_lint_and_then(
93-
cx,
94-
QUESTION_MARK,
95-
expr.span,
96-
"this block may be rewritten with the `?` operator",
97-
|db| {
98-
db.span_suggestion_with_applicability(
99-
expr.span,
100-
"replace_it_with",
101-
replacement_str,
102-
Applicability::MaybeIncorrect, // snippet
103-
);
104-
}
105-
)
92+
93+
if let Some(replacement_str) = replacement {
94+
span_lint_and_then(
95+
cx,
96+
QUESTION_MARK,
97+
expr.span,
98+
"this block may be rewritten with the `?` operator",
99+
|db| {
100+
db.span_suggestion_with_applicability(
101+
expr.span,
102+
"replace_it_with",
103+
replacement_str,
104+
Applicability::MaybeIncorrect, // snippet
105+
);
106+
}
107+
)
108+
}
106109
}
107110
}
108111
}

0 commit comments

Comments
 (0)