@@ -6,6 +6,7 @@ use rustc_ast::{walk_list, Label, Mutability};
6
6
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: def:: Res ;
9
+ use rustc_hir:: definitions:: { DefPathData , DisambiguatedDefPathData } ;
9
10
use rustc_hir:: intravisit:: { walk_expr, FnKind , Visitor } ;
10
11
use rustc_hir:: {
11
12
Arm , Block , Body , Expr , ExprKind , Guard , HirId , Let , Local , Pat , PatKind , Path , PathSegment , QPath , Stmt , StmtKind ,
@@ -22,13 +23,14 @@ use rustc_span::Span;
22
23
declare_clippy_lint ! {
23
24
/// ### What it does
24
25
/// Checks for arguments that are only used in recursion with no side-effects.
26
+ ///
27
+ /// ### Why is this bad?
28
+ /// It could contain a useless calculation and can make function simpler.
29
+ ///
25
30
/// The arguments can be involved in calculations and assignments but as long as
26
31
/// the calculations have no side-effects (function calls or mutating dereference)
27
32
/// and the assigned variables are also only in recursion, it is useless.
28
33
///
29
- /// ### Why is this bad?
30
- /// The could contain a useless calculation and can make function simpler.
31
- ///
32
34
/// ### Known problems
33
35
/// In some cases, this would not catch all useless arguments.
34
36
///
@@ -52,6 +54,8 @@ declare_clippy_lint! {
52
54
/// - some `break` relative operations
53
55
/// - struct pattern binding
54
56
///
57
+ /// Also, when you recurse the function name with path segments, it is not possible to detect.
58
+ ///
55
59
/// ### Example
56
60
/// ```rust
57
61
/// fn f(a: usize, b: usize) -> usize {
@@ -93,9 +97,20 @@ impl<'tcx> LateLintPass<'tcx> for OnlyUsedInRecursion {
93
97
_: & ' tcx rustc_hir:: FnDecl < ' tcx > ,
94
98
body : & ' tcx Body < ' tcx > ,
95
99
_: Span ,
96
- _ : HirId ,
100
+ id : HirId ,
97
101
) {
98
102
if let FnKind :: ItemFn ( ident, ..) | FnKind :: Method ( ident, ..) = kind {
103
+ let data = cx. tcx . def_path ( cx. tcx . hir ( ) . local_def_id ( id) . to_def_id ( ) ) . data ;
104
+ if data. len ( ) > 1 {
105
+ match data. get ( data. len ( ) - 2 ) {
106
+ Some ( DisambiguatedDefPathData {
107
+ data : DefPathData :: Impl ,
108
+ disambiguator,
109
+ } ) if * disambiguator != 0 => return ,
110
+ _ => { } ,
111
+ }
112
+ }
113
+
99
114
let ty_res = cx. typeck_results ( ) ;
100
115
let param_span = body
101
116
. params
0 commit comments