Skip to content

Commit c569d33

Browse files
committed
Auto merge of rust-lang#7108 - rust-lang:fix-return-try-err, r=Manishearth
un-double `return` on try_err This fixes rust-lang#7103 by looking at the parent expression and omitting the "return " in the suggestion when its already a `return` expression. --- changelog: none
2 parents e441b33 + 243dc46 commit c569d33

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

clippy_lints/src/try_err.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
33
use clippy_utils::ty::is_type_diagnostic_item;
4-
use clippy_utils::{differing_macro_contexts, in_macro, is_lang_ctor, match_def_path, paths};
4+
use clippy_utils::{differing_macro_contexts, get_parent_expr, in_macro, is_lang_ctor, match_def_path, paths};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::LangItem::ResultErr;
@@ -102,10 +102,15 @@ impl<'tcx> LateLintPass<'tcx> for TryErr {
102102
} else {
103103
snippet(cx, err_arg.span, "_")
104104
};
105+
let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) {
106+
"" // already returns
107+
} else {
108+
"return "
109+
};
105110
let suggestion = if err_ty == expr_err_ty {
106-
format!("return {}{}{}", prefix, origin_snippet, suffix)
111+
format!("{}{}{}{}", ret_prefix, prefix, origin_snippet, suffix)
107112
} else {
108-
format!("return {}{}.into(){}", prefix, origin_snippet, suffix)
113+
format!("{}{}{}.into(){}", ret_prefix, prefix, origin_snippet, suffix)
109114
};
110115

111116
span_lint_and_sugg(

tests/ui/try_err.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,11 @@ pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
160160

161161
Poll::Ready(None)
162162
}
163+
164+
// Tests that `return` is not duplicated
165+
pub fn try_return(x: bool) -> Result<i32, i32> {
166+
if x {
167+
return Err(42);
168+
}
169+
Ok(0)
170+
}

tests/ui/try_err.rs

+8
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,11 @@ pub fn poll_next(ready: bool) -> Poll<Option<io::Result<()>>> {
160160

161161
Poll::Ready(None)
162162
}
163+
164+
// Tests that `return` is not duplicated
165+
pub fn try_return(x: bool) -> Result<i32, i32> {
166+
if x {
167+
return Err(42)?;
168+
}
169+
Ok(0)
170+
}

tests/ui/try_err.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,11 @@ error: returning an `Err(_)` with the `?` operator
7474
LL | Err(io::ErrorKind::NotFound)?
7575
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `return Poll::Ready(Some(Err(io::ErrorKind::NotFound.into())))`
7676

77-
error: aborting due to 10 previous errors
77+
error: returning an `Err(_)` with the `?` operator
78+
--> $DIR/try_err.rs:167:16
79+
|
80+
LL | return Err(42)?;
81+
| ^^^^^^^^ help: try this: `Err(42)`
82+
83+
error: aborting due to 11 previous errors
7884

0 commit comments

Comments
 (0)