Skip to content

Commit ba2ac3e

Browse files
committed
Auto merge of #7860 - dswij:question-mark-fp, r=giraffate
Fix `question_mark` FP on custom error type Closes #7859 #7840 aims to ignore `question_mark` when the return type is custom, which is [covered here](https://github.com/rust-lang/rust-clippy/blob/df65291edd6b89a241fed483ab165c32df468746/tests/ui/question_mark.rs#L144-L149). But this fails when there is a call in conditional predicate changelog: [`question_mark`] Fix false positive when there is call in conditional predicate
2 parents a48367e + fb0fbad commit ba2ac3e

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,17 @@ impl QuestionMark {
172172
}
173173
}
174174

175-
fn expression_returns_unmodified_err(
176-
cx: &LateContext<'_>,
177-
expression: &Expr<'_>,
178-
origin_hir_id: &Expr<'_>,
179-
) -> bool {
180-
match expression.kind {
175+
fn expression_returns_unmodified_err(cx: &LateContext<'_>, expr: &Expr<'_>, cond_expr: &Expr<'_>) -> bool {
176+
match expr.kind {
181177
ExprKind::Block(block, _) => {
182178
if let Some(return_expression) = Self::return_expression(block) {
183-
return Self::expression_returns_unmodified_err(cx, return_expression, origin_hir_id);
179+
return Self::expression_returns_unmodified_err(cx, return_expression, cond_expr);
184180
}
185181

186182
false
187183
},
188-
ExprKind::Ret(Some(expr)) | ExprKind::Call(expr, _) => {
189-
Self::expression_returns_unmodified_err(cx, expr, origin_hir_id)
190-
},
191-
ExprKind::Path(_) => path_to_local(expression) == path_to_local(origin_hir_id),
184+
ExprKind::Ret(Some(ret_expr)) => Self::expression_returns_unmodified_err(cx, ret_expr, cond_expr),
185+
ExprKind::Path(_) => path_to_local(expr) == path_to_local(cond_expr),
192186
_ => false,
193187
}
194188
}

tests/ui/question_mark.fixed

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ fn func() -> Option<i32> {
104104
Some(0)
105105
}
106106

107-
fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
107+
fn func_returning_result() -> Result<i32, i32> {
108+
Ok(1)
109+
}
110+
111+
fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
108112
let _ = x?;
109113

110114
x?;
@@ -113,9 +117,22 @@ fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
113117
let y = if let Ok(x) = x {
114118
x
115119
} else {
116-
return Err("some error");
120+
return Err(0);
121+
};
122+
123+
// issue #7859
124+
// no warning
125+
let _ = if let Ok(x) = func_returning_result() {
126+
x
127+
} else {
128+
return Err(0);
117129
};
118130

131+
// no warning
132+
if func_returning_result().is_err() {
133+
return func_returning_result();
134+
}
135+
119136
Ok(y)
120137
}
121138

tests/ui/question_mark.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ fn func() -> Option<i32> {
134134
Some(0)
135135
}
136136

137-
fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
137+
fn func_returning_result() -> Result<i32, i32> {
138+
Ok(1)
139+
}
140+
141+
fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
138142
let _ = if let Ok(x) = x { x } else { return x };
139143

140144
if x.is_err() {
@@ -145,9 +149,22 @@ fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
145149
let y = if let Ok(x) = x {
146150
x
147151
} else {
148-
return Err("some error");
152+
return Err(0);
153+
};
154+
155+
// issue #7859
156+
// no warning
157+
let _ = if let Ok(x) = func_returning_result() {
158+
x
159+
} else {
160+
return Err(0);
149161
};
150162

163+
// no warning
164+
if func_returning_result().is_err() {
165+
return func_returning_result();
166+
}
167+
151168
Ok(y)
152169
}
153170

tests/ui/question_mark.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ LL | | }
101101
| |_____^ help: replace it with: `f()?;`
102102

103103
error: this if-let-else may be rewritten with the `?` operator
104-
--> $DIR/question_mark.rs:138:13
104+
--> $DIR/question_mark.rs:142:13
105105
|
106106
LL | let _ = if let Ok(x) = x { x } else { return x };
107107
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x?`
108108

109109
error: this block may be rewritten with the `?` operator
110-
--> $DIR/question_mark.rs:140:5
110+
--> $DIR/question_mark.rs:144:5
111111
|
112112
LL | / if x.is_err() {
113113
LL | | return x;

0 commit comments

Comments
 (0)