Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6597,6 +6597,7 @@ Released 2018-09-13
[`self_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_assignment
[`self_named_constructors`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_named_constructors
[`self_named_module_files`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_named_module_files
[`self_only_used_in_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_only_used_in_recursion
[`semicolon_if_nothing_returned`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
[`semicolon_inside_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_inside_block
[`semicolon_outside_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_outside_block
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
crate::nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES_INFO,
crate::octal_escapes::OCTAL_ESCAPES_INFO,
crate::only_used_in_recursion::ONLY_USED_IN_RECURSION_INFO,
crate::only_used_in_recursion::SELF_ONLY_USED_IN_RECURSION_INFO,
crate::operators::ABSURD_EXTREME_COMPARISONS_INFO,
crate::operators::ARITHMETIC_SIDE_EFFECTS_INFO,
crate::operators::ASSIGN_OP_PATTERN_INFO,
Expand Down
153 changes: 122 additions & 31 deletions clippy_lints/src/only_used_in_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ declare_clippy_lint! {
/// the calculations have no side-effects (function calls or mutating dereference)
/// and the assigned variables are also only in recursion, it is useless.
///
/// ### Example
/// ```no_run
/// fn f(a: usize, b: usize) -> usize {
/// if a == 0 {
/// 1
/// } else {
/// f(a - 1, b + 1)
/// }
/// }
/// # fn main() {
/// # print!("{}", f(1, 1));
/// # }
/// ```
/// Use instead:
/// ```no_run
/// fn f(a: usize) -> usize {
/// if a == 0 {
/// 1
/// } else {
/// f(a - 1)
/// }
/// }
/// # fn main() {
/// # print!("{}", f(1));
/// # }
/// ```
///
/// ### Known problems
/// Too many code paths in the linting code are currently untested and prone to produce false
/// positives or are prone to have performance implications.
Expand Down Expand Up @@ -51,39 +78,90 @@ declare_clippy_lint! {
/// - struct pattern binding
///
/// Also, when you recurse the function name with path segments, it is not possible to detect.
#[clippy::version = "1.61.0"]
pub ONLY_USED_IN_RECURSION,
complexity,
"arguments that is only used in recursion can be removed"
}

declare_clippy_lint! {
/// ### What it does
/// Checks for `self` receiver that is only used in recursion with no side-effects.
///
/// ### Why is this bad?
///
/// It may be possible to remove the `self` argument, allowing the function to be
/// used without an object of type `Self`.
///
/// ### Example
/// ```no_run
/// fn f(a: usize, b: usize) -> usize {
/// if a == 0 {
/// 1
/// } else {
/// f(a - 1, b + 1)
/// struct Foo;
/// impl Foo {
/// fn f(&self, n: u32) -> u32 {
/// if n == 0 {
/// 1
/// } else {
/// n * self.f(n - 1)
/// }
/// }
/// }
/// # fn main() {
/// # print!("{}", f(1, 1));
/// # print!("{}", Foo.f(10));
/// # }
/// ```
/// Use instead:
/// ```no_run
/// fn f(a: usize) -> usize {
/// if a == 0 {
/// 1
/// } else {
/// f(a - 1)
/// struct Foo;
/// impl Foo {
/// fn f(n: u32) -> u32 {
/// if n == 0 {
/// 1
/// } else {
/// n * Self::f(n - 1)
/// }
/// }
/// }
/// # fn main() {
/// # print!("{}", f(1));
/// # print!("{}", Foo::f(10));
/// # }
/// ```
#[clippy::version = "1.61.0"]
pub ONLY_USED_IN_RECURSION,
complexity,
"arguments that is only used in recursion can be removed"
///
/// ### Known problems
/// Too many code paths in the linting code are currently untested and prone to produce false
/// positives or are prone to have performance implications.
///
/// In some cases, this would not catch all useless arguments.
///
/// ```no_run
/// struct Foo;
/// impl Foo {
/// fn foo(&self, a: usize) -> usize {
/// let f = |x| x;
///
/// if a == 0 {
/// 1
/// } else {
/// f(self).foo(a)
/// }
/// }
/// }
/// ```
///
/// For example, here `self` is only used in recursion, but the lint would not catch it.
///
/// List of some examples that can not be caught:
/// - binary operation of non-primitive types
/// - closure usage
/// - some `break` relative operations
/// - struct pattern binding
///
/// Also, when you recurse the function name with path segments, it is not possible to detect.
#[clippy::version = "1.92.0"]
pub SELF_ONLY_USED_IN_RECURSION,
pedantic,
"self receiver only used to recursively call method can be removed"
}
impl_lint_pass!(OnlyUsedInRecursion => [ONLY_USED_IN_RECURSION]);
impl_lint_pass!(OnlyUsedInRecursion => [ONLY_USED_IN_RECURSION, SELF_ONLY_USED_IN_RECURSION]);

#[derive(Clone, Copy)]
enum FnKind {
Expand Down Expand Up @@ -355,26 +433,39 @@ impl<'tcx> LateLintPass<'tcx> for OnlyUsedInRecursion {
self.params.flag_for_linting();
for param in &self.params.params {
if param.apply_lint.get() {
span_lint_and_then(
cx,
ONLY_USED_IN_RECURSION,
param.ident.span,
"parameter is only used in recursion",
|diag| {
if param.ident.name != kw::SelfLower {
if param.ident.name == kw::SelfLower {
span_lint_and_then(
cx,
SELF_ONLY_USED_IN_RECURSION,
param.ident.span,
"`self` is only used in recursion",
|diag| {
diag.span_note(
param.uses.iter().map(|x| x.span).collect::<Vec<_>>(),
"`self` used here",
);
},
);
} else {
span_lint_and_then(
cx,
ONLY_USED_IN_RECURSION,
param.ident.span,
"parameter is only used in recursion",
|diag| {
diag.span_suggestion(
param.ident.span,
"if this is intentional, prefix it with an underscore",
format!("_{}", param.ident.name),
Applicability::MaybeIncorrect,
);
}
diag.span_note(
param.uses.iter().map(|x| x.span).collect::<Vec<_>>(),
"parameter used here",
);
},
);
diag.span_note(
param.uses.iter().map(|x| x.span).collect::<Vec<_>>(),
"parameter used here",
);
},
);
}
}
}
self.params.clear();
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/only_used_in_recursion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(clippy::only_used_in_recursion)]
#![warn(clippy::self_only_used_in_recursion)]
//@no-rustfix
fn _simple(x: u32) -> u32 {
x
Expand Down Expand Up @@ -74,7 +75,7 @@ impl A {
}

fn _method_self(&self, flag: usize, a: usize) -> usize {
//~^ only_used_in_recursion
//~^ self_only_used_in_recursion
//~| only_used_in_recursion

if flag == 0 { 0 } else { self._method_self(flag - 1, a) }
Expand Down
Loading