Fix FP of result_unit_err when using type aliases

This commit is contained in:
Yoshitomo Nakanishi 2021-02-18 18:35:25 +09:00
parent ddeea97714
commit a87fa0e350
3 changed files with 40 additions and 16 deletions

View file

@ -1,7 +1,7 @@
use crate::utils::{
attr_by_name, attrs::is_proc_macro, is_must_use_ty, is_trait_impl_item, is_type_diagnostic_item, iter_input_pats,
last_path_segment, match_def_path, must_use_attr, path_to_local, return_ty, snippet, snippet_opt, span_lint,
span_lint_and_help, span_lint_and_then, trait_ref_of_method, type_is_unsafe_function,
match_def_path, must_use_attr, path_to_local, return_ty, snippet, snippet_opt, span_lint, span_lint_and_help,
span_lint_and_then, trait_ref_of_method, type_is_unsafe_function,
};
use if_chain::if_chain;
use rustc_ast::ast::Attribute;
@ -470,12 +470,11 @@ fn check_result_unit_err(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, item_span
if_chain! {
if !in_external_macro(cx.sess(), item_span);
if let hir::FnRetTy::Return(ref ty) = decl.output;
if let hir::TyKind::Path(ref qpath) = ty.kind;
if is_type_diagnostic_item(cx, hir_ty_to_ty(cx.tcx, ty), sym::result_type);
if let Some(ref args) = last_path_segment(qpath).args;
if let [_, hir::GenericArg::Type(ref err_ty)] = args.args;
if let hir::TyKind::Tup(t) = err_ty.kind;
if t.is_empty();
let ty = hir_ty_to_ty(cx.tcx, ty);
if is_type_diagnostic_item(cx, ty, sym::result_type);
if let ty::Adt(_, substs) = ty.kind();
let err_ty = substs.type_at(1);
if err_ty.is_unit();
then {
span_lint_and_help(
cx,

View file

@ -1,6 +1,4 @@
#![allow(clippy::unnecessary_wraps)]
#[warn(clippy::result_unit_err)]
#[allow(unused)]
#![warn(clippy::result_unit_err)]
pub fn returns_unit_error() -> Result<u32, ()> {
Err(())
@ -36,4 +34,23 @@ impl UnitErrorHolder {
}
}
// https://github.com/rust-lang/rust-clippy/issues/6546
pub mod issue_6546 {
type ResInv<A, B> = Result<B, A>;
pub fn should_lint() -> ResInv<(), usize> {
Ok(0)
}
pub fn should_not_lint() -> ResInv<usize, ()> {
Ok(())
}
type MyRes<A, B> = Result<(A, B), Box<dyn std::error::Error>>;
pub fn should_not_lint2(x: i32) -> MyRes<i32, ()> {
Ok((x, ()))
}
}
fn main() {}

View file

@ -1,5 +1,5 @@
error: this returns a `Result<_, ()>
--> $DIR/result_unit_error.rs:5:1
--> $DIR/result_unit_error.rs:3:1
|
LL | pub fn returns_unit_error() -> Result<u32, ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -8,7 +8,7 @@ LL | pub fn returns_unit_error() -> Result<u32, ()> {
= help: use a custom Error type instead
error: this returns a `Result<_, ()>
--> $DIR/result_unit_error.rs:14:5
--> $DIR/result_unit_error.rs:12:5
|
LL | fn get_that_error(&self) -> Result<bool, ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -16,7 +16,7 @@ LL | fn get_that_error(&self) -> Result<bool, ()>;
= help: use a custom Error type instead
error: this returns a `Result<_, ()>
--> $DIR/result_unit_error.rs:16:5
--> $DIR/result_unit_error.rs:14:5
|
LL | fn get_this_one_too(&self) -> Result<bool, ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -24,12 +24,20 @@ LL | fn get_this_one_too(&self) -> Result<bool, ()> {
= help: use a custom Error type instead
error: this returns a `Result<_, ()>
--> $DIR/result_unit_error.rs:34:5
--> $DIR/result_unit_error.rs:32:5
|
LL | pub fn unit_error(&self) -> Result<usize, ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a custom Error type instead
error: aborting due to 4 previous errors
error: this returns a `Result<_, ()>
--> $DIR/result_unit_error.rs:41:5
|
LL | pub fn should_lint() -> ResInv<(), usize> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a custom Error type instead
error: aborting due to 5 previous errors