|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet; |
| 3 | +use rustc_ast::ast::{Expr, ExprKind}; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_lint::{EarlyContext, EarlyLintPass}; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// ### What it does |
| 10 | + /// |
| 11 | + /// Checks for usage of: |
| 12 | + /// |
| 13 | + /// - `[foo].iter()` |
| 14 | + /// - `[foo].iter_mut()` |
| 15 | + /// - `[foo].into_iter()` |
| 16 | + /// - `Some(foo).iter()` |
| 17 | + /// - `Some(foo).iter_mut()` |
| 18 | + /// - `Some(foo).into_iter()` |
| 19 | + /// |
| 20 | + /// ### Why is this bad? |
| 21 | + /// |
| 22 | + /// It is simpler to use the once function from the standard library: |
| 23 | + /// |
| 24 | + /// ### Example |
| 25 | + /// |
| 26 | + /// ```rust |
| 27 | + /// let a = [123].iter(); |
| 28 | + /// let b = Some(123).into_iter(); |
| 29 | + /// ``` |
| 30 | + /// Use instead: |
| 31 | + /// ```rust |
| 32 | + /// use std::iter; |
| 33 | + /// let a = iter::once(&123); |
| 34 | + /// let b = iter::once(123); |
| 35 | + /// ``` |
| 36 | + /// |
| 37 | + /// ### Known problems |
| 38 | + /// |
| 39 | + /// The type of the resulting iterator might become incompatible with its usage |
| 40 | + #[clippy::version = "1.64.0"] |
| 41 | + pub ITER_ONCE, |
| 42 | + nursery, |
| 43 | + "Iterator for array of length 1" |
| 44 | +} |
| 45 | + |
| 46 | +declare_clippy_lint! { |
| 47 | + /// ### What it does |
| 48 | + /// |
| 49 | + /// Checks for usage of: |
| 50 | + /// |
| 51 | + /// - `[].iter()` |
| 52 | + /// - `[].iter_mut()` |
| 53 | + /// - `[].into_iter()` |
| 54 | + /// - `None.iter()` |
| 55 | + /// - `None.iter_mut()` |
| 56 | + /// - `None.into_iter()` |
| 57 | + /// |
| 58 | + /// ### Why is this bad? |
| 59 | + /// |
| 60 | + /// It is simpler to use the empty function from the standard library: |
| 61 | + /// |
| 62 | + /// ### Example |
| 63 | + /// |
| 64 | + /// ```rust |
| 65 | + /// use std::{slice, option}; |
| 66 | + /// let a: slice::Iter<i32> = [].iter(); |
| 67 | + /// let f: option::IntoIter<i32> = None.into_iter(); |
| 68 | + /// ``` |
| 69 | + /// Use instead: |
| 70 | + /// ```rust |
| 71 | + /// use std::iter; |
| 72 | + /// let a: iter::Empty<i32> = iter::empty(); |
| 73 | + /// let b: iter::Empty<i32> = iter::empty(); |
| 74 | + /// ``` |
| 75 | + /// |
| 76 | + /// ### Known problems |
| 77 | + /// |
| 78 | + /// The type of the resulting iterator might become incompatible with its usage |
| 79 | + #[clippy::version = "1.64.0"] |
| 80 | + pub ITER_EMPTY, |
| 81 | + nursery, |
| 82 | + "Iterator for empty array" |
| 83 | +} |
| 84 | + |
| 85 | +declare_lint_pass!(IterOnceEmpty => [ITER_ONCE, ITER_EMPTY]); |
| 86 | + |
| 87 | +impl EarlyLintPass for IterOnceEmpty { |
| 88 | + fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { |
| 89 | + if expr.span.from_expansion() { |
| 90 | + // Don't lint match expressions present in |
| 91 | + // macro_rules! block |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + let (method_name, args) = if let ExprKind::MethodCall(seg, args, _) = &expr.kind { |
| 96 | + (seg.ident.as_str(), args) |
| 97 | + } else { |
| 98 | + return; |
| 99 | + }; |
| 100 | + let arg = if args.len() == 1 { |
| 101 | + &args[0] |
| 102 | + } else { |
| 103 | + return; |
| 104 | + }; |
| 105 | + |
| 106 | + let item = match &arg.kind { |
| 107 | + ExprKind::Array(v) if v.len() <= 1 => v.first(), |
| 108 | + ExprKind::Path(None, p) => { |
| 109 | + if p.segments.len() == 1 && p.segments[0].ident.name == rustc_span::sym::None { |
| 110 | + None |
| 111 | + } else { |
| 112 | + return; |
| 113 | + } |
| 114 | + }, |
| 115 | + ExprKind::Call(f, some_args) if some_args.len() == 1 => { |
| 116 | + if let ExprKind::Path(None, p) = &f.kind { |
| 117 | + if p.segments.len() == 1 && p.segments[0].ident.name == rustc_span::sym::Some { |
| 118 | + Some(&some_args[0]) |
| 119 | + } else { |
| 120 | + return; |
| 121 | + } |
| 122 | + } else { |
| 123 | + return; |
| 124 | + } |
| 125 | + }, |
| 126 | + _ => return, |
| 127 | + }; |
| 128 | + |
| 129 | + if let Some(i) = item { |
| 130 | + let (sugg, msg) = match method_name { |
| 131 | + "iter" => ( |
| 132 | + format!("std::iter::once(&{})", snippet(cx, i.span, "...")), |
| 133 | + "this `iter` call can be replaced with std::iter::once", |
| 134 | + ), |
| 135 | + "iter_mut" => ( |
| 136 | + format!("std::iter::once(&mut {})", snippet(cx, i.span, "...")), |
| 137 | + "this `iter_mut` call can be replaced with std::iter::once", |
| 138 | + ), |
| 139 | + "into_iter" => ( |
| 140 | + format!("std::iter::once({})", snippet(cx, i.span, "...")), |
| 141 | + "this `into_iter` call can be replaced with std::iter::once", |
| 142 | + ), |
| 143 | + _ => return, |
| 144 | + }; |
| 145 | + span_lint_and_sugg(cx, ITER_ONCE, expr.span, msg, "try", sugg, Applicability::Unspecified); |
| 146 | + } else { |
| 147 | + let msg = match method_name { |
| 148 | + "iter" => "this `iter call` can be replaced with std::iter::empty", |
| 149 | + "iter_mut" => "this `iter_mut` call can be replaced with std::iter::empty", |
| 150 | + "into_iter" => "this `into_iter` call can be replaced with std::iter::empty", |
| 151 | + _ => return, |
| 152 | + }; |
| 153 | + span_lint_and_sugg( |
| 154 | + cx, |
| 155 | + ITER_EMPTY, |
| 156 | + expr.span, |
| 157 | + msg, |
| 158 | + "try", |
| 159 | + "std::iter::empty()".to_string(), |
| 160 | + Applicability::Unspecified, |
| 161 | + ); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments