Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix question_mark FP on custom error type #7860

Merged
merged 5 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,17 @@ impl QuestionMark {
}
}

fn expression_returns_unmodified_err(
cx: &LateContext<'_>,
expression: &Expr<'_>,
origin_hir_id: &Expr<'_>,
) -> bool {
match expression.kind {
fn expression_returns_unmodified_err(cx: &LateContext<'_>, expr: &Expr<'_>, cond_expr: &Expr<'_>) -> bool {
match expr.kind {
ExprKind::Block(block, _) => {
if let Some(return_expression) = Self::return_expression(block) {
return Self::expression_returns_unmodified_err(cx, return_expression, origin_hir_id);
return Self::expression_returns_unmodified_err(cx, return_expression, cond_expr);
}

false
},
ExprKind::Ret(Some(expr)) | ExprKind::Call(expr, _) => {
Self::expression_returns_unmodified_err(cx, expr, origin_hir_id)
},
ExprKind::Path(_) => path_to_local(expression) == path_to_local(origin_hir_id),
ExprKind::Ret(Some(ret_expr)) => Self::expression_returns_unmodified_err(cx, ret_expr, cond_expr),
ExprKind::Path(_) => path_to_local(expr) == path_to_local(cond_expr),
_ => false,
}
}
Expand Down
21 changes: 19 additions & 2 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ fn func() -> Option<i32> {
Some(0)
}

fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
fn func_returning_result() -> Result<i32, i32> {
Ok(1)
}

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

x?;
Expand All @@ -113,9 +117,22 @@ fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
let y = if let Ok(x) = x {
x
} else {
return Err("some error");
return Err(0);
};

// issue #7859
// no warning
let _ = if let Ok(x) = func_returning_result() {
x
} else {
return Err(0);
};

// no warning
if func_returning_result().is_err() {
return func_returning_result();
}

Ok(y)
}

Expand Down
21 changes: 19 additions & 2 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ fn func() -> Option<i32> {
Some(0)
}

fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
fn func_returning_result() -> Result<i32, i32> {
Ok(1)
}

fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
let _ = if let Ok(x) = x { x } else { return x };

if x.is_err() {
Expand All @@ -145,9 +149,22 @@ fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
let y = if let Ok(x) = x {
x
} else {
return Err("some error");
return Err(0);
};

// issue #7859
// no warning
let _ = if let Ok(x) = func_returning_result() {
x
} else {
return Err(0);
};

// no warning
if func_returning_result().is_err() {
return func_returning_result();
}

Ok(y)
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/question_mark.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ LL | | }
| |_____^ help: replace it with: `f()?;`

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

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