Skip to content

Commit 1dacfe6

Browse files
committed
Auto merge of #8422 - buttercrab:only_used_in_recursion, r=llogiq
new lint: `only_used_in_recursion` changelog: - added `only_used_in_recursion`. - fixed code that variables are only used in recursion. - this would not lint when `unused_variable` Issue: #8390
2 parents 8ae74da + 1ad7e70 commit 1dacfe6

12 files changed

+899
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,6 +3355,7 @@ Released 2018-09-13
33553355
[`not_unsafe_ptr_arg_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#not_unsafe_ptr_arg_deref
33563356
[`octal_escapes`]: https://rust-lang.github.io/rust-clippy/master/index.html#octal_escapes
33573357
[`ok_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#ok_expect
3358+
[`only_used_in_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#only_used_in_recursion
33583359
[`op_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#op_ref
33593360
[`option_as_ref_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_as_ref_deref
33603361
[`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
@@ -235,6 +235,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
235235
LintId::of(non_expressive_names::JUST_UNDERSCORES_AND_DIGITS),
236236
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
237237
LintId::of(octal_escapes::OCTAL_ESCAPES),
238+
LintId::of(only_used_in_recursion::ONLY_USED_IN_RECURSION),
238239
LintId::of(open_options::NONSENSICAL_OPEN_OPTIONS),
239240
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),
240241
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
@@ -64,6 +64,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
6464
LintId::of(neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD),
6565
LintId::of(no_effect::NO_EFFECT),
6666
LintId::of(no_effect::UNNECESSARY_OPERATION),
67+
LintId::of(only_used_in_recursion::ONLY_USED_IN_RECURSION),
6768
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
6869
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
6970
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
@@ -399,6 +399,7 @@ store.register_lints(&[
399399
non_send_fields_in_send_ty::NON_SEND_FIELDS_IN_SEND_TY,
400400
nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES,
401401
octal_escapes::OCTAL_ESCAPES,
402+
only_used_in_recursion::ONLY_USED_IN_RECURSION,
402403
open_options::NONSENSICAL_OPEN_OPTIONS,
403404
option_env_unwrap::OPTION_ENV_UNWRAP,
404405
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
@@ -318,6 +318,7 @@ mod non_octal_unix_permissions;
318318
mod non_send_fields_in_send_ty;
319319
mod nonstandard_macro_braces;
320320
mod octal_escapes;
321+
mod only_used_in_recursion;
321322
mod open_options;
322323
mod option_env_unwrap;
323324
mod option_if_let_else;
@@ -857,6 +858,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
857858
store.register_late_pass(move || Box::new(borrow_as_ptr::BorrowAsPtr::new(msrv)));
858859
store.register_late_pass(move || Box::new(manual_bits::ManualBits::new(msrv)));
859860
store.register_late_pass(|| Box::new(default_union_representation::DefaultUnionRepresentation));
861+
store.register_late_pass(|| Box::new(only_used_in_recursion::OnlyUsedInRecursion));
860862
store.register_late_pass(|| Box::new(dbg_macro::DbgMacro));
861863
let cargo_ignore_publish = conf.cargo_ignore_publish;
862864
store.register_late_pass(move || {

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)