Merge remote-tracking branch 'upstream/master' into rustup

This commit is contained in:
Philipp Krones 2023-07-14 13:27:56 +02:00
commit 415fdb2d1a
No known key found for this signature in database
GPG key ID: 1CA0DF2AF59D68A5
522 changed files with 5251 additions and 2556 deletions

View file

@ -72,6 +72,7 @@ mod or_fun_call;
mod or_then_unwrap;
mod path_buf_push_overwrite;
mod range_zip_with_len;
mod read_line_without_trim;
mod repeat_once;
mod search_is_some;
mod seek_from_current;
@ -88,6 +89,7 @@ mod suspicious_command_arg_space;
mod suspicious_map;
mod suspicious_splitn;
mod suspicious_to_owned;
mod type_id_on_box;
mod uninit_assumed_init;
mod unit_hash;
mod unnecessary_filter_map;
@ -2925,6 +2927,37 @@ declare_clippy_lint! {
"use of sort() when sort_unstable() is equivalent"
}
declare_clippy_lint! {
/// ### What it does
/// Looks for calls to `<Box<dyn Any> as Any>::type_id`.
///
/// ### Why is this bad?
/// This most certainly does not do what the user expects and is very easy to miss.
/// Calling `type_id` on a `Box<dyn Any>` calls `type_id` on the `Box<..>` itself,
/// so this will return the `TypeId` of the `Box<dyn Any>` type (not the type id
/// of the value referenced by the box!).
///
/// ### Example
/// ```rust,ignore
/// use std::any::{Any, TypeId};
///
/// let any_box: Box<dyn Any> = Box::new(42_i32);
/// assert_eq!(any_box.type_id(), TypeId::of::<i32>()); // ⚠️ this fails!
/// ```
/// Use instead:
/// ```rust
/// use std::any::{Any, TypeId};
///
/// let any_box: Box<dyn Any> = Box::new(42_i32);
/// assert_eq!((*any_box).type_id(), TypeId::of::<i32>());
/// // ^ dereference first, to call `type_id` on `dyn Any`
/// ```
#[clippy::version = "1.72.0"]
pub TYPE_ID_ON_BOX,
suspicious,
"calling `.type_id()` on `Box<dyn Any>`"
}
declare_clippy_lint! {
/// ### What it does
/// Detects `().hash(_)`.
@ -3316,6 +3349,35 @@ declare_clippy_lint! {
"checks for usage of `Iterator::fold` with a type that implements `Try`"
}
declare_clippy_lint! {
/// Looks for calls to [`Stdin::read_line`] to read a line from the standard input
/// into a string, then later attempting to parse this string into a type without first trimming it, which will
/// always fail because the string has a trailing newline in it.
///
/// ### Why is this bad?
/// The `.parse()` call will always fail.
///
/// ### Example
/// ```rust,ignore
/// let mut input = String::new();
/// std::io::stdin().read_line(&mut input).expect("Failed to read a line");
/// let num: i32 = input.parse().expect("Not a number!");
/// assert_eq!(num, 42); // we never even get here!
/// ```
/// Use instead:
/// ```rust,ignore
/// let mut input = String::new();
/// std::io::stdin().read_line(&mut input).expect("Failed to read a line");
/// let num: i32 = input.trim_end().parse().expect("Not a number!");
/// // ^^^^^^^^^^^ remove the trailing newline
/// assert_eq!(num, 42);
/// ```
#[clippy::version = "1.72.0"]
pub READ_LINE_WITHOUT_TRIM,
correctness,
"calling `Stdin::read_line`, then trying to parse it without first trimming"
}
pub struct Methods {
avoid_breaking_exported_api: bool,
msrv: Msrv,
@ -3389,6 +3451,7 @@ impl_lint_pass!(Methods => [
STRING_EXTEND_CHARS,
ITER_CLONED_COLLECT,
ITER_WITH_DRAIN,
TYPE_ID_ON_BOX,
USELESS_ASREF,
UNNECESSARY_FOLD,
UNNECESSARY_FILTER_MAP,
@ -3435,6 +3498,7 @@ impl_lint_pass!(Methods => [
REPEAT_ONCE,
STABLE_SORT_PRIMITIVE,
UNIT_HASH,
READ_LINE_WITHOUT_TRIM,
UNNECESSARY_SORT_BY,
VEC_RESIZE_TO_ZERO,
VERBOSE_FILE_READS,
@ -3846,6 +3910,9 @@ impl Methods {
("read_to_string", [_]) => {
verbose_file_reads::check(cx, expr, recv, verbose_file_reads::READ_TO_STRING_MSG);
},
("read_line", [arg]) => {
read_line_without_trim::check(cx, expr, recv, arg);
}
("repeat", [arg]) => {
repeat_once::check(cx, expr, recv, arg);
},
@ -3914,6 +3981,9 @@ impl Methods {
("to_os_string" | "to_path_buf" | "to_vec", []) => {
implicit_clone::check(cx, name, expr, recv);
},
("type_id", []) => {
type_id_on_box::check(cx, recv, expr.span);
}
("unwrap", []) => {
match method_call(recv) {
Some(("get", recv, [get_arg], _, _)) => {
@ -3949,7 +4019,7 @@ impl Methods {
}
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
},
("unwrap_or_default", []) => {
("unwrap_or_default" | "unwrap_unchecked" | "unwrap_err_unchecked", []) => {
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
}
("unwrap_or_else", [u_arg]) => {
@ -4134,7 +4204,7 @@ impl SelfKind {
};
let Some(trait_def_id) = cx.tcx.get_diagnostic_item(trait_sym) else {
return false
return false;
};
implements_trait(cx, ty, trait_def_id, &[parent_ty.into()])
}