@@ -6,6 +6,7 @@ use rustc_ast::{walk_list, Label, Mutability};
66use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
77use rustc_errors:: Applicability ;
88use rustc_hir:: def:: Res ;
9+ use rustc_hir:: definitions:: { DefPathData , DisambiguatedDefPathData } ;
910use rustc_hir:: intravisit:: { walk_expr, FnKind , Visitor } ;
1011use rustc_hir:: {
1112 Arm , Block , Body , Expr , ExprKind , Guard , HirId , Let , Local , Pat , PatKind , Path , PathSegment , QPath , Stmt , StmtKind ,
@@ -22,13 +23,14 @@ use rustc_span::Span;
2223declare_clippy_lint ! {
2324 /// ### What it does
2425 /// 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+ ///
2530 /// The arguments can be involved in calculations and assignments but as long as
2631 /// the calculations have no side-effects (function calls or mutating dereference)
2732 /// and the assigned variables are also only in recursion, it is useless.
2833 ///
29- /// ### Why is this bad?
30- /// The could contain a useless calculation and can make function simpler.
31- ///
3234 /// ### Known problems
3335 /// In some cases, this would not catch all useless arguments.
3436 ///
@@ -52,6 +54,8 @@ declare_clippy_lint! {
5254 /// - some `break` relative operations
5355 /// - struct pattern binding
5456 ///
57+ /// Also, when you recurse the function name with path segments, it is not possible to detect.
58+ ///
5559 /// ### Example
5660 /// ```rust
5761 /// fn f(a: usize, b: usize) -> usize {
@@ -93,9 +97,20 @@ impl<'tcx> LateLintPass<'tcx> for OnlyUsedInRecursion {
9397 _: & ' tcx rustc_hir:: FnDecl < ' tcx > ,
9498 body : & ' tcx Body < ' tcx > ,
9599 _: Span ,
96- _ : HirId ,
100+ id : HirId ,
97101 ) {
98102 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+
99114 let ty_res = cx. typeck_results ( ) ;
100115 let param_span = body
101116 . params
0 commit comments