Skip to content

Commit 7d535f6

Browse files
committed
Auto merge of rust-lang#6759 - Y-Nak:fix-fp-of-result_unit_err, r=llogiq
Fix FP of result_unit_err when using type aliases fixes rust-lang#6546 changelog: none
2 parents ddeea97 + a87fa0e commit 7d535f6

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

clippy_lints/src/functions.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::{
22
attr_by_name, attrs::is_proc_macro, is_must_use_ty, is_trait_impl_item, is_type_diagnostic_item, iter_input_pats,
3-
last_path_segment, match_def_path, must_use_attr, path_to_local, return_ty, snippet, snippet_opt, span_lint,
4-
span_lint_and_help, span_lint_and_then, trait_ref_of_method, type_is_unsafe_function,
3+
match_def_path, must_use_attr, path_to_local, return_ty, snippet, snippet_opt, span_lint, span_lint_and_help,
4+
span_lint_and_then, trait_ref_of_method, type_is_unsafe_function,
55
};
66
use if_chain::if_chain;
77
use rustc_ast::ast::Attribute;
@@ -470,12 +470,11 @@ fn check_result_unit_err(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, item_span
470470
if_chain! {
471471
if !in_external_macro(cx.sess(), item_span);
472472
if let hir::FnRetTy::Return(ref ty) = decl.output;
473-
if let hir::TyKind::Path(ref qpath) = ty.kind;
474-
if is_type_diagnostic_item(cx, hir_ty_to_ty(cx.tcx, ty), sym::result_type);
475-
if let Some(ref args) = last_path_segment(qpath).args;
476-
if let [_, hir::GenericArg::Type(ref err_ty)] = args.args;
477-
if let hir::TyKind::Tup(t) = err_ty.kind;
478-
if t.is_empty();
473+
let ty = hir_ty_to_ty(cx.tcx, ty);
474+
if is_type_diagnostic_item(cx, ty, sym::result_type);
475+
if let ty::Adt(_, substs) = ty.kind();
476+
let err_ty = substs.type_at(1);
477+
if err_ty.is_unit();
479478
then {
480479
span_lint_and_help(
481480
cx,

tests/ui/result_unit_error.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#![allow(clippy::unnecessary_wraps)]
2-
#[warn(clippy::result_unit_err)]
3-
#[allow(unused)]
1+
#![warn(clippy::result_unit_err)]
42

53
pub fn returns_unit_error() -> Result<u32, ()> {
64
Err(())
@@ -36,4 +34,23 @@ impl UnitErrorHolder {
3634
}
3735
}
3836

37+
// https://github.com/rust-lang/rust-clippy/issues/6546
38+
pub mod issue_6546 {
39+
type ResInv<A, B> = Result<B, A>;
40+
41+
pub fn should_lint() -> ResInv<(), usize> {
42+
Ok(0)
43+
}
44+
45+
pub fn should_not_lint() -> ResInv<usize, ()> {
46+
Ok(())
47+
}
48+
49+
type MyRes<A, B> = Result<(A, B), Box<dyn std::error::Error>>;
50+
51+
pub fn should_not_lint2(x: i32) -> MyRes<i32, ()> {
52+
Ok((x, ()))
53+
}
54+
}
55+
3956
fn main() {}

tests/ui/result_unit_error.stderr

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this returns a `Result<_, ()>
2-
--> $DIR/result_unit_error.rs:5:1
2+
--> $DIR/result_unit_error.rs:3:1
33
|
44
LL | pub fn returns_unit_error() -> Result<u32, ()> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,28 +8,36 @@ LL | pub fn returns_unit_error() -> Result<u32, ()> {
88
= help: use a custom Error type instead
99

1010
error: this returns a `Result<_, ()>
11-
--> $DIR/result_unit_error.rs:14:5
11+
--> $DIR/result_unit_error.rs:12:5
1212
|
1313
LL | fn get_that_error(&self) -> Result<bool, ()>;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
1616
= help: use a custom Error type instead
1717

1818
error: this returns a `Result<_, ()>
19-
--> $DIR/result_unit_error.rs:16:5
19+
--> $DIR/result_unit_error.rs:14:5
2020
|
2121
LL | fn get_this_one_too(&self) -> Result<bool, ()> {
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323
|
2424
= help: use a custom Error type instead
2525

2626
error: this returns a `Result<_, ()>
27-
--> $DIR/result_unit_error.rs:34:5
27+
--> $DIR/result_unit_error.rs:32:5
2828
|
2929
LL | pub fn unit_error(&self) -> Result<usize, ()> {
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3131
|
3232
= help: use a custom Error type instead
3333

34-
error: aborting due to 4 previous errors
34+
error: this returns a `Result<_, ()>
35+
--> $DIR/result_unit_error.rs:41:5
36+
|
37+
LL | pub fn should_lint() -> ResInv<(), usize> {
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
|
40+
= help: use a custom Error type instead
41+
42+
error: aborting due to 5 previous errors
3543

0 commit comments

Comments
 (0)