Skip to content

Commit 4f96ca3

Browse files
committed
add only_used_in_recursion lint
- fix code that have variables that is "only used in recursion" - add test
1 parent 6966a42 commit 4f96ca3

10 files changed

+732
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3349,6 +3349,7 @@ Released 2018-09-13
33493349
[`not_unsafe_ptr_arg_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#not_unsafe_ptr_arg_deref
33503350
[`octal_escapes`]: https://rust-lang.github.io/rust-clippy/master/index.html#octal_escapes
33513351
[`ok_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#ok_expect
3352+
[`only_used_in_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#only_used_in_recursion
33523353
[`op_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#op_ref
33533354
[`option_as_ref_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_as_ref_deref
33543355
[`option_env_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_env_unwrap

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
226226
LintId::of(non_expressive_names::JUST_UNDERSCORES_AND_DIGITS),
227227
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
228228
LintId::of(octal_escapes::OCTAL_ESCAPES),
229+
LintId::of(only_used_in_recursion::ONLY_USED_IN_RECURSION),
229230
LintId::of(open_options::NONSENSICAL_OPEN_OPTIONS),
230231
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),
231232
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),

clippy_lints/src/lib.register_complexity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
6363
LintId::of(neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD),
6464
LintId::of(no_effect::NO_EFFECT),
6565
LintId::of(no_effect::UNNECESSARY_OPERATION),
66+
LintId::of(only_used_in_recursion::ONLY_USED_IN_RECURSION),
6667
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
6768
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
6869
LintId::of(precedence::PRECEDENCE),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ store.register_lints(&[
388388
non_send_fields_in_send_ty::NON_SEND_FIELDS_IN_SEND_TY,
389389
nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES,
390390
octal_escapes::OCTAL_ESCAPES,
391+
only_used_in_recursion::ONLY_USED_IN_RECURSION,
391392
open_options::NONSENSICAL_OPEN_OPTIONS,
392393
option_env_unwrap::OPTION_ENV_UNWRAP,
393394
option_if_let_else::OPTION_IF_LET_ELSE,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ mod non_octal_unix_permissions;
316316
mod non_send_fields_in_send_ty;
317317
mod nonstandard_macro_braces;
318318
mod octal_escapes;
319+
mod only_used_in_recursion;
319320
mod open_options;
320321
mod option_env_unwrap;
321322
mod option_if_let_else;
@@ -862,6 +863,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
862863
store.register_late_pass(move || Box::new(borrow_as_ptr::BorrowAsPtr::new(msrv)));
863864
store.register_late_pass(move || Box::new(manual_bits::ManualBits::new(msrv)));
864865
store.register_late_pass(|| Box::new(default_union_representation::DefaultUnionRepresentation));
866+
store.register_late_pass(|| Box::new(only_used_in_recursion::OnlyUsedInRecursion));
865867
// add lints here, do not remove this comment, it's used in `new_lint`
866868
}
867869

clippy_lints/src/methods/option_map_or_none.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@ use super::RESULT_MAP_OR_INTO_OPTION;
1414

1515
// The expression inside a closure may or may not have surrounding braces
1616
// which causes problems when generating a suggestion.
17-
fn reduce_unit_expression<'a>(
18-
cx: &LateContext<'_>,
19-
expr: &'a hir::Expr<'_>,
20-
) -> Option<(&'a hir::Expr<'a>, &'a [hir::Expr<'a>])> {
17+
fn reduce_unit_expression<'a>(expr: &'a hir::Expr<'_>) -> Option<(&'a hir::Expr<'a>, &'a [hir::Expr<'a>])> {
2118
match expr.kind {
2219
hir::ExprKind::Call(func, arg_char) => Some((func, arg_char)),
2320
hir::ExprKind::Block(block, _) => {
2421
match (block.stmts, block.expr) {
2522
(&[], Some(inner_expr)) => {
2623
// If block only contains an expression,
2724
// reduce `|x| { x + 1 }` to `|x| x + 1`
28-
reduce_unit_expression(cx, inner_expr)
25+
reduce_unit_expression(inner_expr)
2926
},
3027
_ => None,
3128
}
@@ -77,7 +74,7 @@ pub(super) fn check<'tcx>(
7774
if let hir::ExprKind::Closure(_, _, id, span, _) = map_arg.kind;
7875
let arg_snippet = snippet(cx, span, "..");
7976
let body = cx.tcx.hir().body(id);
80-
if let Some((func, [arg_char])) = reduce_unit_expression(cx, &body.value);
77+
if let Some((func, [arg_char])) = reduce_unit_expression(&body.value);
8178
if let Some(id) = path_def_id(cx, func).and_then(|ctor_id| cx.tcx.parent(ctor_id));
8279
if Some(id) == cx.tcx.lang_items().option_some_variant();
8380
then {

0 commit comments

Comments
 (0)