Skip to content

Commit 01ca66c

Browse files
committed
Fix FP on question_mark if returned object is not local
1 parent a5d5976 commit 01ca66c

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
@@ -183,7 +183,7 @@ impl QuestionMark {
183183
false
184184
},
185185
ExprKind::Ret(Some(ret_expr)) => Self::expression_returns_unmodified_err(cx, ret_expr, cond_expr),
186-
ExprKind::Path(_) => path_to_local(expr) == path_to_local(cond_expr),
186+
ExprKind::Path(_) => path_to_local(expr).is_some() && path_to_local(expr) == path_to_local(cond_expr),
187187
_ => false,
188188
}
189189
}

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)