Skip to content

Commit 1c87b2d

Browse files
committed
check for semicolon
1 parent 1abff3f commit 1c87b2d

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::higher;
33
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::ty::is_type_diagnostic_item;
6-
use clippy_utils::{eq_expr_value, is_lang_ctor, path_to_local, peel_blocks, peel_blocks_with_stmt};
6+
use clippy_utils::{eq_expr_value, is_lang_ctor, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt};
77
use if_chain::if_chain;
88
use rustc_errors::Applicability;
99
use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr, ResultOk};
@@ -101,14 +101,19 @@ impl QuestionMark {
101101
// Only check one of the blocks
102102
let nested_expr = if_else.unwrap_or(if_then);
103103
if Self::check_lang_items(cx, path1, &[OptionSome, ResultOk, ResultErr]);
104-
if let PatKind::Binding(annot, _, ident, _) = fields[0].kind;
104+
if let PatKind::Binding(annot, bind_id, ident, _) = fields[0].kind;
105105
if Self::result_check_and_early_return(cx, let_expr, nested_expr, Some(ident.name))
106106
|| Self::option_check_and_early_return(cx, let_expr, nested_expr);
107107
let by_ref = matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut);
108108
then {
109109
let mut applicability = Applicability::MachineApplicable;
110110
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
111-
let replacement = format!("{}{}?;", receiver_str, if by_ref { ".as_ref()" } else { "" },);
111+
let replacement = format!(
112+
"{}{}?{}",
113+
receiver_str,
114+
if by_ref { ".as_ref()" } else { "" },
115+
if path_to_local_id(peel_blocks(if_then), bind_id) { "" } else { ";" }
116+
);
112117

113118
span_lint_and_sugg(
114119
cx,
@@ -178,8 +183,10 @@ impl QuestionMark {
178183
symbol: Option<Symbol>,
179184
) -> bool {
180185
match &peel_blocks_with_stmt(expr).kind {
181-
ExprKind::Ret(Some(ret_expr)) => Self::expression_returns_unmodified_err(cx, ret_expr, cond_expr, symbol),
182-
ExprKind::Path(_) => path_to_local(expr).is_some() && path_to_local(expr) == path_to_local(cond_expr),
186+
ExprKind::Ret(Some(ret_expr)) =>
187+
Self::expression_returns_unmodified_err(cx, ret_expr, cond_expr, symbol),
188+
ExprKind::Path(_) =>
189+
path_to_local(expr).is_some() && path_to_local(expr) == path_to_local(cond_expr),
183190
ExprKind::Call(_, args_expr) => {
184191
if let Some(arg) = args_expr.first() {
185192
if let Some(name) = symbol {

tests/ui/question_mark.fixed

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl CopyStruct {
5252

5353
let _ = Some(self.opt?);
5454

55-
let _ = self.opt?;;
55+
let _ = self.opt?;
5656

5757
self.opt
5858
}
@@ -82,13 +82,13 @@ impl MoveStruct {
8282
}
8383

8484
pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
85-
let v: &Vec<_> = self.opt.as_ref()?;;
85+
let v: &Vec<_> = self.opt.as_ref()?;
8686

8787
Some(v.clone())
8888
}
8989

9090
pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
91-
let v = self.opt?;;
91+
let v = self.opt?;
9292

9393
Some(v)
9494
}
@@ -109,7 +109,7 @@ fn func_returning_result() -> Result<i32, i32> {
109109
}
110110

111111
fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
112-
let _ = x?;;
112+
let _ = x?;
113113

114114
x?;
115115

tests/ui/question_mark.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ LL | | x
4444
LL | | } else {
4545
LL | | return None;
4646
LL | | };
47-
| |_________^ help: replace it with: `self.opt?;`
47+
| |_________^ help: replace it with: `self.opt?`
4848

4949
error: this block may be rewritten with the `?` operator
5050
--> $DIR/question_mark.rs:82:9
@@ -79,7 +79,7 @@ LL | | v
7979
LL | | } else {
8080
LL | | return None;
8181
LL | | };
82-
| |_________^ help: replace it with: `self.opt.as_ref()?;`
82+
| |_________^ help: replace it with: `self.opt.as_ref()?`
8383

8484
error: this if-let-else may be rewritten with the `?` operator
8585
--> $DIR/question_mark.rs:115:17
@@ -90,7 +90,7 @@ LL | | v
9090
LL | | } else {
9191
LL | | return None;
9292
LL | | };
93-
| |_________^ help: replace it with: `self.opt?;`
93+
| |_________^ help: replace it with: `self.opt?`
9494

9595
error: this block may be rewritten with the `?` operator
9696
--> $DIR/question_mark.rs:130:5
@@ -104,7 +104,7 @@ error: this if-let-else may be rewritten with the `?` operator
104104
--> $DIR/question_mark.rs:142:13
105105
|
106106
LL | let _ = if let Ok(x) = x { x } else { return x };
107-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x?;`
107+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x?`
108108

109109
error: this block may be rewritten with the `?` operator
110110
--> $DIR/question_mark.rs:144:5

0 commit comments

Comments
 (0)