Skip to content

Commit c609d29

Browse files
authored
new_ret_no_self: extract to a separate module (#15798)
changelog: none
2 parents aa0b914 + 195ff23 commit c609d29

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ mod needless_character_iteration;
8080
mod needless_collect;
8181
mod needless_option_as_deref;
8282
mod needless_option_take;
83+
mod new_ret_no_self;
8384
mod no_effect_replace;
8485
mod obfuscated_if_else;
8586
mod ok_expect;
@@ -148,11 +149,9 @@ mod zst_offset;
148149

149150
use clippy_config::Conf;
150151
use clippy_utils::consts::{ConstEvalCtxt, Constant};
151-
use clippy_utils::diagnostics::span_lint;
152152
use clippy_utils::macros::FormatArgsStorage;
153153
use clippy_utils::msrvs::{self, Msrv};
154-
use clippy_utils::ty::contains_ty_adt_constructor_opaque;
155-
use clippy_utils::{contains_return, is_trait_method, iter_input_pats, peel_blocks, return_ty, sym};
154+
use clippy_utils::{contains_return, is_trait_method, iter_input_pats, peel_blocks, sym};
156155
pub use path_ends_with_ext::DEFAULT_ALLOWED_DOTFILES;
157156
use rustc_data_structures::fx::FxHashSet;
158157
use rustc_hir::{self as hir, Expr, ExprKind, Node, Stmt, StmtKind, TraitItem, TraitItemKind};
@@ -4921,20 +4920,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
49214920
);
49224921
}
49234922

4924-
// if this impl block implements a trait, lint in trait definition instead
4925-
if !implements_trait
4926-
&& impl_item.ident.name == sym::new
4927-
&& let ret_ty = return_ty(cx, impl_item.owner_id)
4928-
&& ret_ty != self_ty
4929-
&& !contains_ty_adt_constructor_opaque(cx, ret_ty, self_ty)
4930-
{
4931-
span_lint(
4932-
cx,
4933-
NEW_RET_NO_SELF,
4934-
impl_item.span,
4935-
"methods called `new` usually return `Self`",
4936-
);
4937-
}
4923+
new_ret_no_self::check_impl_item(cx, impl_item, self_ty, implements_trait);
49384924
}
49394925
}
49404926

@@ -4966,18 +4952,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
49664952
);
49674953
}
49684954

4969-
if item.ident.name == sym::new
4970-
&& let ret_ty = return_ty(cx, item.owner_id)
4971-
&& let self_ty = TraitRef::identity(cx.tcx, item.owner_id.to_def_id()).self_ty()
4972-
&& !ret_ty.contains(self_ty)
4973-
{
4974-
span_lint(
4975-
cx,
4976-
NEW_RET_NO_SELF,
4977-
item.span,
4978-
"methods called `new` usually return `Self`",
4979-
);
4980-
}
4955+
new_ret_no_self::check_trait_item(cx, item);
49814956
}
49824957
}
49834958
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use clippy_utils::diagnostics::span_lint;
2+
use clippy_utils::return_ty;
3+
use clippy_utils::ty::contains_ty_adt_constructor_opaque;
4+
use rustc_hir::{ImplItem, TraitItem};
5+
use rustc_lint::LateContext;
6+
use rustc_middle::ty::{self, Ty};
7+
use rustc_span::sym;
8+
9+
use super::NEW_RET_NO_SELF;
10+
11+
pub(super) fn check_impl_item<'tcx>(
12+
cx: &LateContext<'tcx>,
13+
impl_item: &'tcx ImplItem<'_>,
14+
self_ty: Ty<'tcx>,
15+
implements_trait: bool,
16+
) {
17+
// if this impl block implements a trait, lint in trait definition instead
18+
if !implements_trait
19+
&& impl_item.ident.name == sym::new
20+
&& let ret_ty = return_ty(cx, impl_item.owner_id)
21+
&& ret_ty != self_ty
22+
&& !contains_ty_adt_constructor_opaque(cx, ret_ty, self_ty)
23+
{
24+
span_lint(
25+
cx,
26+
NEW_RET_NO_SELF,
27+
impl_item.span,
28+
"methods called `new` usually return `Self`",
29+
);
30+
}
31+
}
32+
33+
pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, trait_item: &'tcx TraitItem<'tcx>) {
34+
if trait_item.ident.name == sym::new
35+
&& let ret_ty = return_ty(cx, trait_item.owner_id)
36+
&& let self_ty = ty::TraitRef::identity(cx.tcx, trait_item.owner_id.to_def_id()).self_ty()
37+
&& !ret_ty.contains(self_ty)
38+
{
39+
span_lint(
40+
cx,
41+
NEW_RET_NO_SELF,
42+
trait_item.span,
43+
"methods called `new` usually return `Self`",
44+
);
45+
}
46+
}

0 commit comments

Comments
 (0)