Skip to content

Commit f615ea4

Browse files
committed
Auto merge of #8080 - dswij:8019, r=giraffate
Fix FP on `question_mark` if returned object is not local Closes #8019 changelog: [`question_mark`] Fix FP when returned object is not local
2 parents 48d939f + 01ca66c commit f615ea4

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl QuestionMark {
159159
fn expression_returns_unmodified_err(cx: &LateContext<'_>, expr: &Expr<'_>, cond_expr: &Expr<'_>) -> bool {
160160
match peel_blocks_with_stmt(expr).kind {
161161
ExprKind::Ret(Some(ret_expr)) => Self::expression_returns_unmodified_err(cx, ret_expr, cond_expr),
162-
ExprKind::Path(_) => path_to_local(expr) == path_to_local(cond_expr),
162+
ExprKind::Path(_) => path_to_local(expr).is_some() && path_to_local(expr) == path_to_local(cond_expr),
163163
_ => false,
164164
}
165165
}

tests/ui/question_mark.fixed

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
136136
Ok(y)
137137
}
138138

139+
// see issue #8019
140+
pub enum NotOption {
141+
None,
142+
First,
143+
AfterFirst,
144+
}
145+
146+
fn obj(_: i32) -> Result<(), NotOption> {
147+
Err(NotOption::First)
148+
}
149+
150+
fn f() -> NotOption {
151+
if obj(2).is_err() {
152+
return NotOption::None;
153+
}
154+
NotOption::First
155+
}
156+
139157
fn main() {
140158
some_func(Some(42));
141159
some_func(None);
@@ -157,4 +175,5 @@ fn main() {
157175
func();
158176

159177
let _ = result_func(Ok(42));
178+
let _ = f();
160179
}

tests/ui/question_mark.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
168168
Ok(y)
169169
}
170170

171+
// see issue #8019
172+
pub enum NotOption {
173+
None,
174+
First,
175+
AfterFirst,
176+
}
177+
178+
fn obj(_: i32) -> Result<(), NotOption> {
179+
Err(NotOption::First)
180+
}
181+
182+
fn f() -> NotOption {
183+
if obj(2).is_err() {
184+
return NotOption::None;
185+
}
186+
NotOption::First
187+
}
188+
171189
fn main() {
172190
some_func(Some(42));
173191
some_func(None);
@@ -189,4 +207,5 @@ fn main() {
189207
func();
190208

191209
let _ = result_func(Ok(42));
210+
let _ = f();
192211
}

0 commit comments

Comments
 (0)