Merge commit 'd7b5cbf065' into clippyup
This commit is contained in:
parent
bd071bf5b2
commit
f8f9d01c2a
199 changed files with 4158 additions and 1931 deletions
|
|
@ -7,9 +7,22 @@ use rustc_lint::LateContext;
|
|||
use rustc_span::symbol::sym;
|
||||
|
||||
/// Checks for `for` loops over `Option`s and `Result`s.
|
||||
pub(super) fn check(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>) {
|
||||
pub(super) fn check(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>, method_name: Option<&str>) {
|
||||
let ty = cx.typeck_results().expr_ty(arg);
|
||||
if is_type_diagnostic_item(cx, ty, sym::Option) {
|
||||
let help_string = if let Some(method_name) = method_name {
|
||||
format!(
|
||||
"consider replacing `for {0} in {1}.{method_name}()` with `if let Some({0}) = {1}`",
|
||||
snippet(cx, pat.span, "_"),
|
||||
snippet(cx, arg.span, "_")
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"consider replacing `for {0} in {1}` with `if let Some({0}) = {1}`",
|
||||
snippet(cx, pat.span, "_"),
|
||||
snippet(cx, arg.span, "_")
|
||||
)
|
||||
};
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
FOR_LOOPS_OVER_FALLIBLES,
|
||||
|
|
@ -20,13 +33,22 @@ pub(super) fn check(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>) {
|
|||
snippet(cx, arg.span, "_")
|
||||
),
|
||||
None,
|
||||
&format!(
|
||||
"consider replacing `for {0} in {1}` with `if let Some({0}) = {1}`",
|
||||
snippet(cx, pat.span, "_"),
|
||||
snippet(cx, arg.span, "_")
|
||||
),
|
||||
&help_string,
|
||||
);
|
||||
} else if is_type_diagnostic_item(cx, ty, sym::Result) {
|
||||
let help_string = if let Some(method_name) = method_name {
|
||||
format!(
|
||||
"consider replacing `for {0} in {1}.{method_name}()` with `if let Ok({0}) = {1}`",
|
||||
snippet(cx, pat.span, "_"),
|
||||
snippet(cx, arg.span, "_")
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"consider replacing `for {0} in {1}` with `if let Ok({0}) = {1}`",
|
||||
snippet(cx, pat.span, "_"),
|
||||
snippet(cx, arg.span, "_")
|
||||
)
|
||||
};
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
FOR_LOOPS_OVER_FALLIBLES,
|
||||
|
|
@ -37,11 +59,7 @@ pub(super) fn check(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>) {
|
|||
snippet(cx, arg.span, "_")
|
||||
),
|
||||
None,
|
||||
&format!(
|
||||
"consider replacing `for {0} in {1}` with `if let Ok({0}) = {1}`",
|
||||
snippet(cx, pat.span, "_"),
|
||||
snippet(cx, arg.span, "_")
|
||||
),
|
||||
&help_string,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,7 +42,8 @@ declare_clippy_lint! {
|
|||
/// dst[i + 64] = src[i];
|
||||
/// }
|
||||
/// ```
|
||||
/// Could be written as:
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// # let src = vec![1];
|
||||
/// # let mut dst = vec![0; 65];
|
||||
|
|
@ -70,7 +71,8 @@ declare_clippy_lint! {
|
|||
/// println!("{}", vec[i]);
|
||||
/// }
|
||||
/// ```
|
||||
/// Could be written as:
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// let vec = vec!['a', 'b', 'c'];
|
||||
/// for i in vec {
|
||||
|
|
@ -103,7 +105,8 @@ declare_clippy_lint! {
|
|||
/// // ..
|
||||
/// }
|
||||
/// ```
|
||||
/// can be rewritten to
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// # let y = vec![1];
|
||||
/// for x in &y {
|
||||
|
|
@ -188,6 +191,10 @@ declare_clippy_lint! {
|
|||
/// for x in &res {
|
||||
/// // ..
|
||||
/// }
|
||||
///
|
||||
/// for x in res.iter() {
|
||||
/// // ..
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
|
|
@ -282,7 +289,8 @@ declare_clippy_lint! {
|
|||
/// i += 1;
|
||||
/// }
|
||||
/// ```
|
||||
/// Could be written as
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// # let v = vec![1];
|
||||
/// # fn bar(bar: usize, baz: usize) {}
|
||||
|
|
@ -469,7 +477,7 @@ declare_clippy_lint! {
|
|||
///
|
||||
/// ### Why is this bad?
|
||||
/// This kind of operation can be expressed more succinctly with
|
||||
/// `vec![item;SIZE]` or `vec.resize(NEW_SIZE, item)` and using these alternatives may also
|
||||
/// `vec![item; SIZE]` or `vec.resize(NEW_SIZE, item)` and using these alternatives may also
|
||||
/// have better performance.
|
||||
///
|
||||
/// ### Example
|
||||
|
|
@ -484,7 +492,8 @@ declare_clippy_lint! {
|
|||
/// vec.push(item2);
|
||||
/// }
|
||||
/// ```
|
||||
/// could be written as
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// let item1 = 2;
|
||||
/// let item2 = 3;
|
||||
|
|
@ -512,7 +521,8 @@ declare_clippy_lint! {
|
|||
/// println!("{}", item);
|
||||
/// }
|
||||
/// ```
|
||||
/// could be written as
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// let item1 = 2;
|
||||
/// let item = &item1;
|
||||
|
|
@ -586,7 +596,7 @@ declare_clippy_lint! {
|
|||
/// std::hint::spin_loop()
|
||||
/// }
|
||||
/// ```
|
||||
#[clippy::version = "1.59.0"]
|
||||
#[clippy::version = "1.61.0"]
|
||||
pub MISSING_SPIN_LOOP,
|
||||
perf,
|
||||
"An empty busy waiting loop"
|
||||
|
|
@ -695,10 +705,14 @@ fn check_for_loop_arg(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>) {
|
|||
let method_name = method.ident.as_str();
|
||||
// check for looping over x.iter() or x.iter_mut(), could use &x or &mut x
|
||||
match method_name {
|
||||
"iter" | "iter_mut" => explicit_iter_loop::check(cx, self_arg, arg, method_name),
|
||||
"iter" | "iter_mut" => {
|
||||
explicit_iter_loop::check(cx, self_arg, arg, method_name);
|
||||
for_loops_over_fallibles::check(cx, pat, self_arg, Some(method_name));
|
||||
},
|
||||
"into_iter" => {
|
||||
explicit_iter_loop::check(cx, self_arg, arg, method_name);
|
||||
explicit_into_iter_loop::check(cx, self_arg, arg);
|
||||
for_loops_over_fallibles::check(cx, pat, self_arg, Some(method_name));
|
||||
},
|
||||
"next" => {
|
||||
next_loop_linted = iter_next_loop::check(cx, arg);
|
||||
|
|
@ -708,6 +722,6 @@ fn check_for_loop_arg(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>) {
|
|||
}
|
||||
|
||||
if !next_loop_linted {
|
||||
for_loops_over_fallibles::check(cx, pat, arg);
|
||||
for_loops_over_fallibles::check(cx, pat, arg, None);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -117,13 +117,20 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
|
|||
| ExprKind::Type(e, _)
|
||||
| ExprKind::Field(e, _)
|
||||
| ExprKind::AddrOf(_, _, e)
|
||||
| ExprKind::Struct(_, _, Some(e))
|
||||
| ExprKind::Repeat(e, _)
|
||||
| ExprKind::DropTemps(e) => never_loop_expr(e, main_loop_id),
|
||||
ExprKind::Let(let_expr) => never_loop_expr(let_expr.init, main_loop_id),
|
||||
ExprKind::Array(es) | ExprKind::MethodCall(_, es, _) | ExprKind::Tup(es) => {
|
||||
never_loop_expr_all(&mut es.iter(), main_loop_id)
|
||||
},
|
||||
ExprKind::Struct(_, fields, base) => {
|
||||
let fields = never_loop_expr_all(&mut fields.iter().map(|f| f.expr), main_loop_id);
|
||||
if let Some(base) = base {
|
||||
combine_both(fields, never_loop_expr(base, main_loop_id))
|
||||
} else {
|
||||
fields
|
||||
}
|
||||
},
|
||||
ExprKind::Call(e, es) => never_loop_expr_all(&mut once(e).chain(es.iter()), main_loop_id),
|
||||
ExprKind::Binary(_, e1, e2)
|
||||
| ExprKind::Assign(e1, e2, _)
|
||||
|
|
@ -180,8 +187,7 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
|
|||
| InlineAsmOperand::SymStatic { .. } => NeverLoopResult::Otherwise,
|
||||
})
|
||||
.fold(NeverLoopResult::Otherwise, combine_both),
|
||||
ExprKind::Struct(_, _, None)
|
||||
| ExprKind::Yield(_, _)
|
||||
ExprKind::Yield(_, _)
|
||||
| ExprKind::Closure { .. }
|
||||
| ExprKind::Path(_)
|
||||
| ExprKind::ConstBlock(_)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue