Skip to content

Commit

Permalink
Auto merge of rust-lang#113417 - flip1995:clippy_beta_backport, r=Man…
Browse files Browse the repository at this point in the history
…ishearth

[beta] Clippy beta backport

Backported PRs:

- rust-lang/rust-clippy#10813
- rust-lang/rust-clippy#10865
- rust-lang/rust-clippy#10873
- rust-lang/rust-clippy#10992

Those address bugs, that were either introduced with 1.71 and that we would like to address before they get into stable or bugs that were introduced in 1.70 and that we would like to be fixed in 1.71 already.

Detailed arguments for the backports of those PRs are in the PRs (mostly).

r? `@Mark-Simulacrum`
  • Loading branch information
bors committed Jul 6, 2023
2 parents dbf31f1 + 8cde275 commit f06c1b8
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::{diagnostics::span_lint_and_sugg, match_def_path, paths};
use clippy_utils::{diagnostics::span_lint_and_sugg, is_ty_alias, match_def_path, paths};
use hir::{def::Res, ExprKind};
use rustc_errors::Applicability;
use rustc_hir as hir;
Expand Down Expand Up @@ -43,12 +43,23 @@ declare_clippy_lint! {
}
declare_lint_pass!(DefaultConstructedUnitStructs => [DEFAULT_CONSTRUCTED_UNIT_STRUCTS]);

fn is_alias(ty: hir::Ty<'_>) -> bool {
if let hir::TyKind::Path(ref qpath) = ty.kind {
is_ty_alias(qpath)
} else {
false
}
}

impl LateLintPass<'_> for DefaultConstructedUnitStructs {
fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
if_chain!(
// make sure we have a call to `Default::default`
if let hir::ExprKind::Call(fn_expr, &[]) = expr.kind;
if let ExprKind::Path(ref qpath@ hir::QPath::TypeRelative(_,_)) = fn_expr.kind;
if let ExprKind::Path(ref qpath @ hir::QPath::TypeRelative(base, _)) = fn_expr.kind;
// make sure this isn't a type alias:
// `<Foo as Bar>::Assoc` cannot be used as a constructor
if !is_alias(*base);
if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id);
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
// make sure we have a struct with no fields (unit struct)
Expand Down
3 changes: 2 additions & 1 deletion src/tools/clippy/clippy_lints/src/items_after_test_module.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::{diagnostics::span_lint_and_help, is_in_cfg_test};
use clippy_utils::{diagnostics::span_lint_and_help, is_from_proc_macro, is_in_cfg_test};
use rustc_hir::{HirId, ItemId, ItemKind, Mod};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
Expand Down Expand Up @@ -59,6 +59,7 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
if !matches!(item.kind, ItemKind::Mod(_));
if !is_in_cfg_test(cx.tcx, itid.hir_id()); // The item isn't in the testing module itself
if !in_external_macro(cx.sess(), item.span);
if !is_from_proc_macro(cx, item);

then {
span_lint_and_help(cx, ITEMS_AFTER_TEST_MODULE, test_mod_span.unwrap().with_hi(item.span.hi()), "items were found after the testing module", None, "move the items to before the testing module was defined");
Expand Down
12 changes: 10 additions & 2 deletions src/tools/clippy/clippy_lints/src/let_with_type_underscore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::source::snippet;
use rustc_hir::{Local, TyKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::lint::in_external_macro;
Expand All @@ -25,14 +26,21 @@ declare_clippy_lint! {
declare_lint_pass!(UnderscoreTyped => [LET_WITH_TYPE_UNDERSCORE]);

impl LateLintPass<'_> for UnderscoreTyped {
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
if_chain! {
if !in_external_macro(cx.tcx.sess, local.span);
if let Some(ty) = local.ty; // Ensure that it has a type defined
if let TyKind::Infer = &ty.kind; // that type is '_'
if local.span.ctxt() == ty.span.ctxt();
then {
span_lint_and_help(cx,
// NOTE: Using `is_from_proc_macro` on `init` will require that it's initialized,
// this doesn't. Alternatively, `WithSearchPat` can be implemented for `Ty`
if snippet(cx, ty.span, "_").trim() != "_" {
return;
}

span_lint_and_help(
cx,
LET_WITH_TYPE_UNDERSCORE,
local.span,
"variable declared with type underscore",
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/redundant_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ declare_clippy_lint! {
/// ```
#[clippy::version = "1.32.0"]
pub REDUNDANT_CLONE,
perf,
nursery,
"`clone()` of an owned value that is going to be dropped immediately"
}

Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub fn is_wild(pat: &Pat<'_>) -> bool {
/// Checks if the given `QPath` belongs to a type alias.
pub fn is_ty_alias(qpath: &QPath<'_>) -> bool {
match *qpath {
QPath::Resolved(_, path) => matches!(path.res, Res::Def(DefKind::TyAlias, ..)),
QPath::Resolved(_, path) => matches!(path.res, Res::Def(DefKind::TyAlias | DefKind::AssocTy, ..)),
QPath::TypeRelative(ty, _) if let TyKind::Path(qpath) = ty.kind => { is_ty_alias(&qpath) },
_ => false,
}
Expand Down
22 changes: 22 additions & 0 deletions src/tools/clippy/tests/ui/default_constructed_unit_structs.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ struct EmptyStruct {}
#[non_exhaustive]
struct NonExhaustiveStruct;

mod issue_10755 {
struct Sqlite {}

trait HasArguments<'q> {
type Arguments;
}

impl<'q> HasArguments<'q> for Sqlite {
type Arguments = std::marker::PhantomData<&'q ()>;
}

type SqliteArguments<'q> = <Sqlite as HasArguments<'q>>::Arguments;

fn foo() {
// should not lint
// type alias cannot be used as a constructor
let _ = <Sqlite as HasArguments>::Arguments::default();

let _ = SqliteArguments::default();
}
}

fn main() {
// should lint
let _ = PhantomData::<usize>;
Expand Down
22 changes: 22 additions & 0 deletions src/tools/clippy/tests/ui/default_constructed_unit_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ struct EmptyStruct {}
#[non_exhaustive]
struct NonExhaustiveStruct;

mod issue_10755 {
struct Sqlite {}

trait HasArguments<'q> {
type Arguments;
}

impl<'q> HasArguments<'q> for Sqlite {
type Arguments = std::marker::PhantomData<&'q ()>;
}

type SqliteArguments<'q> = <Sqlite as HasArguments<'q>>::Arguments;

fn foo() {
// should not lint
// type alias cannot be used as a constructor
let _ = <Sqlite as HasArguments>::Arguments::default();

let _ = SqliteArguments::default();
}
}

fn main() {
// should lint
let _ = PhantomData::<usize>::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ LL | inner: PhantomData::default(),
| ^^^^^^^^^^^ help: remove this call to `default`

error: use of `default` to create a unit struct
--> $DIR/default_constructed_unit_structs.rs:106:33
--> $DIR/default_constructed_unit_structs.rs:128:33
|
LL | let _ = PhantomData::<usize>::default();
| ^^^^^^^^^^^ help: remove this call to `default`

error: use of `default` to create a unit struct
--> $DIR/default_constructed_unit_structs.rs:107:42
--> $DIR/default_constructed_unit_structs.rs:129:42
|
LL | let _: PhantomData<i32> = PhantomData::default();
| ^^^^^^^^^^^ help: remove this call to `default`

error: use of `default` to create a unit struct
--> $DIR/default_constructed_unit_structs.rs:108:55
--> $DIR/default_constructed_unit_structs.rs:130:55
|
LL | let _: PhantomData<i32> = std::marker::PhantomData::default();
| ^^^^^^^^^^^ help: remove this call to `default`

error: use of `default` to create a unit struct
--> $DIR/default_constructed_unit_structs.rs:109:23
--> $DIR/default_constructed_unit_structs.rs:131:23
|
LL | let _ = UnitStruct::default();
| ^^^^^^^^^^^ help: remove this call to `default`
Expand Down
25 changes: 24 additions & 1 deletion src/tools/clippy/tests/ui/let_with_type_underscore.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
//@aux-build: proc_macros.rs
#![allow(unused)]
#![warn(clippy::let_with_type_underscore)]
#![allow(clippy::let_unit_value)]
#![allow(clippy::let_unit_value, clippy::needless_late_init)]

extern crate proc_macros;

fn func() -> &'static str {
""
}

#[rustfmt::skip]
fn main() {
// Will lint
let x: _ = 1;
let _: _ = 2;
let x: _ = func();
let x: _;
x = ();

let x = 1; // Will not lint, Rust infers this to an integer before Clippy
let x = func();
let x: Vec<_> = Vec::<u32>::new();
let x: [_; 1] = [1];
let x : _ = 1;

// Do not lint from procedural macros
proc_macros::with_span! {
span
let x: _ = ();
// Late initialization
let x: _;
x = ();
// Ensure weird formatting will not break it (hopefully)
let x : _ = 1;
let x
: _ = 1;
let x :
_;
x = ();
};
}
38 changes: 31 additions & 7 deletions src/tools/clippy/tests/ui/let_with_type_underscore.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,63 @@
error: variable declared with type underscore
--> $DIR/let_with_type_underscore.rs:11:5
--> $DIR/let_with_type_underscore.rs:15:5
|
LL | let x: _ = 1;
| ^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
--> $DIR/let_with_type_underscore.rs:11:10
--> $DIR/let_with_type_underscore.rs:15:10
|
LL | let x: _ = 1;
| ^^^
= note: `-D clippy::let-with-type-underscore` implied by `-D warnings`

error: variable declared with type underscore
--> $DIR/let_with_type_underscore.rs:12:5
--> $DIR/let_with_type_underscore.rs:16:5
|
LL | let _: _ = 2;
| ^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
--> $DIR/let_with_type_underscore.rs:12:10
--> $DIR/let_with_type_underscore.rs:16:10
|
LL | let _: _ = 2;
| ^^^

error: variable declared with type underscore
--> $DIR/let_with_type_underscore.rs:13:5
--> $DIR/let_with_type_underscore.rs:17:5
|
LL | let x: _ = func();
| ^^^^^^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
--> $DIR/let_with_type_underscore.rs:13:10
--> $DIR/let_with_type_underscore.rs:17:10
|
LL | let x: _ = func();
| ^^^

error: aborting due to 3 previous errors
error: variable declared with type underscore
--> $DIR/let_with_type_underscore.rs:18:5
|
LL | let x: _;
| ^^^^^^^^^
|
help: remove the explicit type `_` declaration
--> $DIR/let_with_type_underscore.rs:18:10
|
LL | let x: _;
| ^^^

error: variable declared with type underscore
--> $DIR/let_with_type_underscore.rs:25:5
|
LL | let x : _ = 1;
| ^^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
--> $DIR/let_with_type_underscore.rs:25:10
|
LL | let x : _ = 1;
| ^^^^

error: aborting due to 5 previous errors

1 change: 1 addition & 0 deletions src/tools/clippy/tests/ui/redundant_clone.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@run-rustfix
// rustfix-only-machine-applicable
#![feature(lint_reasons)]
#![warn(clippy::redundant_clone)]
#![allow(clippy::drop_non_drop, clippy::implicit_clone, clippy::uninlined_format_args)]

use std::ffi::OsString;
Expand Down
1 change: 1 addition & 0 deletions src/tools/clippy/tests/ui/redundant_clone.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@run-rustfix
// rustfix-only-machine-applicable
#![feature(lint_reasons)]
#![warn(clippy::redundant_clone)]
#![allow(clippy::drop_non_drop, clippy::implicit_clone, clippy::uninlined_format_args)]

use std::ffi::OsString;
Expand Down
Loading

0 comments on commit f06c1b8

Please sign in to comment.