Skip to content

Commit 357c6a7

Browse files
committed
Auto merge of rust-lang#6646 - nahuakang:for_loops_over_options_or_results, r=flip1995
New Lint: Manual Flatten This is a draft PR for [Issue 6564](rust-lang/rust-clippy#6564). r? `@camsteffen` - \[x] Followed [lint naming conventions][lint_naming] - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[x] Added lint documentation - \[x] Run `cargo dev fmt` --- *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: Add new lint [`manual_flatten`] to check for loops over a single `if let` expression with `Result` or `Option`.
2 parents 876ffa4 + 2f8a8d3 commit 357c6a7

12 files changed

+343
-49
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,7 @@ Released 2018-09-13
20392039
[`manual_async_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_async_fn
20402040
[`manual_filter_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_filter_map
20412041
[`manual_find_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_find_map
2042+
[`manual_flatten`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_flatten
20422043
[`manual_memcpy`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy
20432044
[`manual_non_exhaustive`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive
20442045
[`manual_ok_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_ok_or

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
687687
&loops::FOR_KV_MAP,
688688
&loops::FOR_LOOPS_OVER_FALLIBLES,
689689
&loops::ITER_NEXT_LOOP,
690+
&loops::MANUAL_FLATTEN,
690691
&loops::MANUAL_MEMCPY,
691692
&loops::MUT_RANGE_BOUND,
692693
&loops::NEEDLESS_COLLECT,
@@ -1491,6 +1492,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14911492
LintId::of(&loops::FOR_KV_MAP),
14921493
LintId::of(&loops::FOR_LOOPS_OVER_FALLIBLES),
14931494
LintId::of(&loops::ITER_NEXT_LOOP),
1495+
LintId::of(&loops::MANUAL_FLATTEN),
14941496
LintId::of(&loops::MANUAL_MEMCPY),
14951497
LintId::of(&loops::MUT_RANGE_BOUND),
14961498
LintId::of(&loops::NEEDLESS_COLLECT),
@@ -1822,6 +1824,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
18221824
LintId::of(&lifetimes::EXTRA_UNUSED_LIFETIMES),
18231825
LintId::of(&lifetimes::NEEDLESS_LIFETIMES),
18241826
LintId::of(&loops::EXPLICIT_COUNTER_LOOP),
1827+
LintId::of(&loops::MANUAL_FLATTEN),
18251828
LintId::of(&loops::MUT_RANGE_BOUND),
18261829
LintId::of(&loops::SINGLE_ELEMENT_LOOP),
18271830
LintId::of(&loops::WHILE_LET_LOOP),

clippy_lints/src/loops.rs

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use crate::utils::usage::{is_unused, mutated_variables};
55
use crate::utils::visitors::LocalUsedVisitor;
66
use crate::utils::{
77
contains_name, get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
8-
indent_of, is_in_panic_handler, is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item,
9-
last_path_segment, match_trait_method, match_type, match_var, multispan_sugg, single_segment_path, snippet,
10-
snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg,
11-
span_lint_and_then, sugg, SpanlessEq,
8+
indent_of, is_in_panic_handler, is_integer_const, is_no_std_crate, is_ok_ctor, is_refutable, is_some_ctor,
9+
is_type_diagnostic_item, last_path_segment, match_trait_method, match_type, match_var, multispan_sugg,
10+
single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
11+
span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg, SpanlessEq,
1212
};
1313
use if_chain::if_chain;
1414
use rustc_ast::ast;
@@ -494,8 +494,40 @@ declare_clippy_lint! {
494494
"there is no reason to have a single element loop"
495495
}
496496

497+
declare_clippy_lint! {
498+
/// **What it does:** Check for unnecessary `if let` usage in a for loop
499+
/// where only the `Some` or `Ok` variant of the iterator element is used.
500+
///
501+
/// **Why is this bad?** It is verbose and can be simplified
502+
/// by first calling the `flatten` method on the `Iterator`.
503+
///
504+
/// **Known problems:** None.
505+
///
506+
/// **Example:**
507+
///
508+
/// ```rust
509+
/// let x = vec![Some(1), Some(2), Some(3)];
510+
/// for n in x {
511+
/// if let Some(n) = n {
512+
/// println!("{}", n);
513+
/// }
514+
/// }
515+
/// ```
516+
/// Use instead:
517+
/// ```rust
518+
/// let x = vec![Some(1), Some(2), Some(3)];
519+
/// for n in x.into_iter().flatten() {
520+
/// println!("{}", n);
521+
/// }
522+
/// ```
523+
pub MANUAL_FLATTEN,
524+
complexity,
525+
"for loops over `Option`s or `Result`s with a single expression can be simplified"
526+
}
527+
497528
declare_lint_pass!(Loops => [
498529
MANUAL_MEMCPY,
530+
MANUAL_FLATTEN,
499531
NEEDLESS_RANGE_LOOP,
500532
EXPLICIT_ITER_LOOP,
501533
EXPLICIT_INTO_ITER_LOOP,
@@ -517,14 +549,14 @@ declare_lint_pass!(Loops => [
517549
impl<'tcx> LateLintPass<'tcx> for Loops {
518550
#[allow(clippy::too_many_lines)]
519551
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
520-
if let Some((pat, arg, body)) = higher::for_loop(expr) {
552+
if let Some((pat, arg, body, span)) = higher::for_loop(expr) {
521553
// we don't want to check expanded macros
522554
// this check is not at the top of the function
523555
// since higher::for_loop expressions are marked as expansions
524556
if body.span.from_expansion() {
525557
return;
526558
}
527-
check_for_loop(cx, pat, arg, body, expr);
559+
check_for_loop(cx, pat, arg, body, expr, span);
528560
}
529561

530562
// we don't want to check expanded macros
@@ -819,6 +851,7 @@ fn check_for_loop<'tcx>(
819851
arg: &'tcx Expr<'_>,
820852
body: &'tcx Expr<'_>,
821853
expr: &'tcx Expr<'_>,
854+
span: Span,
822855
) {
823856
let is_manual_memcpy_triggered = detect_manual_memcpy(cx, pat, arg, body, expr);
824857
if !is_manual_memcpy_triggered {
@@ -830,6 +863,7 @@ fn check_for_loop<'tcx>(
830863
check_for_mut_range_bound(cx, arg, body);
831864
check_for_single_element_loop(cx, pat, arg, body, expr);
832865
detect_same_item_push(cx, pat, arg, body, expr);
866+
check_manual_flatten(cx, pat, arg, body, span);
833867
}
834868

835869
// this function assumes the given expression is a `for` loop.
@@ -1953,6 +1987,79 @@ fn check_for_single_element_loop<'tcx>(
19531987
}
19541988
}
19551989

1990+
/// Check for unnecessary `if let` usage in a for loop where only the `Some` or `Ok` variant of the
1991+
/// iterator element is used.
1992+
fn check_manual_flatten<'tcx>(
1993+
cx: &LateContext<'tcx>,
1994+
pat: &'tcx Pat<'_>,
1995+
arg: &'tcx Expr<'_>,
1996+
body: &'tcx Expr<'_>,
1997+
span: Span,
1998+
) {
1999+
if let ExprKind::Block(ref block, _) = body.kind {
2000+
// Ensure the `if let` statement is the only expression or statement in the for-loop
2001+
let inner_expr = if block.stmts.len() == 1 && block.expr.is_none() {
2002+
let match_stmt = &block.stmts[0];
2003+
if let StmtKind::Semi(inner_expr) = match_stmt.kind {
2004+
Some(inner_expr)
2005+
} else {
2006+
None
2007+
}
2008+
} else if block.stmts.is_empty() {
2009+
block.expr
2010+
} else {
2011+
None
2012+
};
2013+
2014+
if_chain! {
2015+
if let Some(inner_expr) = inner_expr;
2016+
if let ExprKind::Match(
2017+
ref match_expr, ref match_arms, MatchSource::IfLetDesugar{ contains_else_clause: false }
2018+
) = inner_expr.kind;
2019+
// Ensure match_expr in `if let` statement is the same as the pat from the for-loop
2020+
if let PatKind::Binding(_, pat_hir_id, _, _) = pat.kind;
2021+
if let ExprKind::Path(QPath::Resolved(None, match_expr_path)) = match_expr.kind;
2022+
if let Res::Local(match_expr_path_id) = match_expr_path.res;
2023+
if pat_hir_id == match_expr_path_id;
2024+
// Ensure the `if let` statement is for the `Some` variant of `Option` or the `Ok` variant of `Result`
2025+
if let PatKind::TupleStruct(QPath::Resolved(None, path), _, _) = match_arms[0].pat.kind;
2026+
let some_ctor = is_some_ctor(cx, path.res);
2027+
let ok_ctor = is_ok_ctor(cx, path.res);
2028+
if some_ctor || ok_ctor;
2029+
let if_let_type = if some_ctor { "Some" } else { "Ok" };
2030+
2031+
then {
2032+
// Prepare the error message
2033+
let msg = format!("unnecessary `if let` since only the `{}` variant of the iterator element is used", if_let_type);
2034+
2035+
// Prepare the help message
2036+
let mut applicability = Applicability::MaybeIncorrect;
2037+
let arg_snippet = make_iterator_snippet(cx, arg, &mut applicability);
2038+
2039+
span_lint_and_then(
2040+
cx,
2041+
MANUAL_FLATTEN,
2042+
span,
2043+
&msg,
2044+
|diag| {
2045+
let sugg = format!("{}.flatten()", arg_snippet);
2046+
diag.span_suggestion(
2047+
arg.span,
2048+
"try",
2049+
sugg,
2050+
Applicability::MaybeIncorrect,
2051+
);
2052+
diag.span_help(
2053+
inner_expr.span,
2054+
"...and remove the `if let` statement in the for loop",
2055+
);
2056+
}
2057+
);
2058+
}
2059+
}
2060+
}
2061+
}
2062+
19562063
struct MutatePairDelegate<'a, 'tcx> {
19572064
cx: &'a LateContext<'tcx>,
19582065
hir_id_low: Option<HirId>,

clippy_lints/src/mut_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
5252
return;
5353
}
5454

55-
if let Some((_, arg, body)) = higher::for_loop(expr) {
55+
if let Some((_, arg, body, _)) = higher::for_loop(expr) {
5656
// A `for` loop lowers to:
5757
// ```rust
5858
// match ::std::iter::Iterator::next(&mut iter) {

clippy_lints/src/needless_question_mark.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use rustc_errors::Applicability;
2-
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
32
use rustc_hir::{Body, Expr, ExprKind, LangItem, MatchSource, QPath};
43
use rustc_lint::{LateContext, LateLintPass, LintContext};
5-
use rustc_middle::ty::DefIdTree;
64
use rustc_semver::RustcVersion;
75
use rustc_session::{declare_tool_lint, impl_lint_pass};
86
use rustc_span::sym;
@@ -160,7 +158,7 @@ fn is_some_or_ok_call<'a>(
160158
// Check outer expression matches CALL_IDENT(ARGUMENT) format
161159
if let ExprKind::Call(path, args) = &expr.kind;
162160
if let ExprKind::Path(QPath::Resolved(None, path)) = &path.kind;
163-
if is_some_ctor(cx, path.res) || is_ok_ctor(cx, path.res);
161+
if utils::is_some_ctor(cx, path.res) || utils::is_ok_ctor(cx, path.res);
164162

165163
// Extract inner expression from ARGUMENT
166164
if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar) = &args[0].kind;
@@ -208,25 +206,3 @@ fn is_some_or_ok_call<'a>(
208206
fn has_implicit_error_from(cx: &LateContext<'_>, entire_expr: &Expr<'_>, inner_result_expr: &Expr<'_>) -> bool {
209207
return cx.typeck_results().expr_ty(entire_expr) != cx.typeck_results().expr_ty(inner_result_expr);
210208
}
211-
212-
fn is_ok_ctor(cx: &LateContext<'_>, res: Res) -> bool {
213-
if let Some(ok_id) = cx.tcx.lang_items().result_ok_variant() {
214-
if let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), id) = res {
215-
if let Some(variant_id) = cx.tcx.parent(id) {
216-
return variant_id == ok_id;
217-
}
218-
}
219-
}
220-
false
221-
}
222-
223-
fn is_some_ctor(cx: &LateContext<'_>, res: Res) -> bool {
224-
if let Some(some_id) = cx.tcx.lang_items().option_some_variant() {
225-
if let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), id) = res {
226-
if let Some(variant_id) = cx.tcx.parent(id) {
227-
return variant_id == some_id;
228-
}
229-
}
230-
}
231-
false
232-
}

clippy_lints/src/ranges.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) {
442442
let mut cur_expr = expr;
443443
while let Some(parent_expr) = get_parent_expr(cx, cur_expr) {
444444
match higher::for_loop(parent_expr) {
445-
Some((_, args, _)) if args.hir_id == expr.hir_id => return true,
445+
Some((_, args, _, _)) if args.hir_id == expr.hir_id => return true,
446446
_ => cur_expr = parent_expr,
447447
}
448448
}

clippy_lints/src/utils/higher.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_ast::ast;
99
use rustc_hir as hir;
1010
use rustc_hir::{BorrowKind, Expr, ExprKind, StmtKind, UnOp};
1111
use rustc_lint::LateContext;
12+
use rustc_span::source_map::Span;
1213

1314
/// Converts a hir binary operator to the corresponding `ast` type.
1415
#[must_use]
@@ -133,11 +134,11 @@ pub fn is_from_for_desugar(local: &hir::Local<'_>) -> bool {
133134
false
134135
}
135136

136-
/// Recover the essential nodes of a desugared for loop:
137-
/// `for pat in arg { body }` becomes `(pat, arg, body)`.
137+
/// Recover the essential nodes of a desugared for loop as well as the entire span:
138+
/// `for pat in arg { body }` becomes `(pat, arg, body)`. Return `(pat, arg, body, span)`.
138139
pub fn for_loop<'tcx>(
139140
expr: &'tcx hir::Expr<'tcx>,
140-
) -> Option<(&hir::Pat<'_>, &'tcx hir::Expr<'tcx>, &'tcx hir::Expr<'tcx>)> {
141+
) -> Option<(&hir::Pat<'_>, &'tcx hir::Expr<'tcx>, &'tcx hir::Expr<'tcx>, Span)> {
141142
if_chain! {
142143
if let hir::ExprKind::Match(ref iterexpr, ref arms, hir::MatchSource::ForLoopDesugar) = expr.kind;
143144
if let hir::ExprKind::Call(_, ref iterargs) = iterexpr.kind;
@@ -148,7 +149,7 @@ pub fn for_loop<'tcx>(
148149
if let hir::StmtKind::Local(ref local) = let_stmt.kind;
149150
if let hir::StmtKind::Expr(ref expr) = body.kind;
150151
then {
151-
return Some((&*local.pat, &iterargs[0], expr));
152+
return Some((&*local.pat, &iterargs[0], expr, arms[0].span));
152153
}
153154
}
154155
None

clippy_lints/src/utils/internal_lints.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -841,15 +841,13 @@ pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool {
841841
// implementations of native types. Check lang items.
842842
let path_syms: Vec<_> = path.iter().map(|p| Symbol::intern(p)).collect();
843843
let lang_items = cx.tcx.lang_items();
844-
for lang_item in lang_items.items() {
845-
if let Some(def_id) = lang_item {
846-
let lang_item_path = cx.get_def_path(*def_id);
847-
if path_syms.starts_with(&lang_item_path) {
848-
if let [item] = &path_syms[lang_item_path.len()..] {
849-
for child in cx.tcx.item_children(*def_id) {
850-
if child.ident.name == *item {
851-
return true;
852-
}
844+
for item_def_id in lang_items.items().iter().flatten() {
845+
let lang_item_path = cx.get_def_path(*item_def_id);
846+
if path_syms.starts_with(&lang_item_path) {
847+
if let [item] = &path_syms[lang_item_path.len()..] {
848+
for child in cx.tcx.item_children(*item_def_id) {
849+
if child.ident.name == *item {
850+
return true;
853851
}
854852
}
855853
}

clippy_lints/src/utils/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc_ast::ast::{self, Attribute, LitKind};
3737
use rustc_data_structures::fx::FxHashMap;
3838
use rustc_errors::Applicability;
3939
use rustc_hir as hir;
40-
use rustc_hir::def::{DefKind, Res};
40+
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
4141
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
4242
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
4343
use rustc_hir::Node;
@@ -50,7 +50,7 @@ use rustc_lint::{LateContext, Level, Lint, LintContext};
5050
use rustc_middle::hir::exports::Export;
5151
use rustc_middle::hir::map::Map;
5252
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
53-
use rustc_middle::ty::{self, layout::IntegerExt, Ty, TyCtxt, TypeFoldable};
53+
use rustc_middle::ty::{self, layout::IntegerExt, DefIdTree, Ty, TyCtxt, TypeFoldable};
5454
use rustc_semver::RustcVersion;
5555
use rustc_session::Session;
5656
use rustc_span::hygiene::{ExpnKind, MacroKind};
@@ -1703,6 +1703,30 @@ pub fn is_hir_ty_cfg_dependant(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {
17031703
}
17041704
}
17051705

1706+
/// Check if the resolution of a given path is an `Ok` variant of `Result`.
1707+
pub fn is_ok_ctor(cx: &LateContext<'_>, res: Res) -> bool {
1708+
if let Some(ok_id) = cx.tcx.lang_items().result_ok_variant() {
1709+
if let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), id) = res {
1710+
if let Some(variant_id) = cx.tcx.parent(id) {
1711+
return variant_id == ok_id;
1712+
}
1713+
}
1714+
}
1715+
false
1716+
}
1717+
1718+
/// Check if the resolution of a given path is a `Some` variant of `Option`.
1719+
pub fn is_some_ctor(cx: &LateContext<'_>, res: Res) -> bool {
1720+
if let Some(some_id) = cx.tcx.lang_items().option_some_variant() {
1721+
if let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), id) = res {
1722+
if let Some(variant_id) = cx.tcx.parent(id) {
1723+
return variant_id == some_id;
1724+
}
1725+
}
1726+
}
1727+
false
1728+
}
1729+
17061730
#[cfg(test)]
17071731
mod test {
17081732
use super::{reindent_multiline, without_block_comments};

clippy_lints/src/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
5555

5656
// search for `for _ in vec![…]`
5757
if_chain! {
58-
if let Some((_, arg, _)) = higher::for_loop(expr);
58+
if let Some((_, arg, _, _)) = higher::for_loop(expr);
5959
if let Some(vec_args) = higher::vec_macro(cx, arg);
6060
if is_copy(cx, vec_type(cx.typeck_results().expr_ty_adjusted(arg)));
6161
then {

0 commit comments

Comments
 (0)