Skip to content

Commit e2e492c

Browse files
committed
Auto merge of rust-lang#8422 - buttercrab:only_used_in_recursion, r=llogiq
new lint: `only_used_in_recursion` changed: - added `only_used_in_recursion`. - fixed code that variables are only used in recursion. - this would not lint when `unused_variable` This fixes: rust-lang#8390 ----- changelog: add lint [`only_used_in_recursion`]
2 parents 75b616e + 800f66d commit e2e492c

12 files changed

+903
-39
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3356,6 +3356,7 @@ Released 2018-09-13
33563356
[`not_unsafe_ptr_arg_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#not_unsafe_ptr_arg_deref
33573357
[`octal_escapes`]: https://rust-lang.github.io/rust-clippy/master/index.html#octal_escapes
33583358
[`ok_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#ok_expect
3359+
[`only_used_in_recursion`]: https://rust-lang.github.io/rust-clippy/master/index.html#only_used_in_recursion
33593360
[`op_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#op_ref
33603361
[`option_as_ref_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_as_ref_deref
33613362
[`option_env_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_env_unwrap

clippy_lints/src/lib.register_all.rs

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
236236
LintId::of(non_expressive_names::JUST_UNDERSCORES_AND_DIGITS),
237237
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
238238
LintId::of(octal_escapes::OCTAL_ESCAPES),
239+
LintId::of(only_used_in_recursion::ONLY_USED_IN_RECURSION),
239240
LintId::of(open_options::NONSENSICAL_OPEN_OPTIONS),
240241
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),
241242
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),

clippy_lints/src/lib.register_complexity.rs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
6565
LintId::of(neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD),
6666
LintId::of(no_effect::NO_EFFECT),
6767
LintId::of(no_effect::UNNECESSARY_OPERATION),
68+
LintId::of(only_used_in_recursion::ONLY_USED_IN_RECURSION),
6869
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
6970
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
7071
LintId::of(precedence::PRECEDENCE),

clippy_lints/src/lib.register_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ store.register_lints(&[
400400
non_send_fields_in_send_ty::NON_SEND_FIELDS_IN_SEND_TY,
401401
nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES,
402402
octal_escapes::OCTAL_ESCAPES,
403+
only_used_in_recursion::ONLY_USED_IN_RECURSION,
403404
open_options::NONSENSICAL_OPEN_OPTIONS,
404405
option_env_unwrap::OPTION_ENV_UNWRAP,
405406
option_if_let_else::OPTION_IF_LET_ELSE,

clippy_lints/src/lib.rs

+2
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

+3-6
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)