Skip to content

Commit f06c1b8

Browse files
committed
Auto merge of #113417 - flip1995:clippy_beta_backport, r=Manishearth
[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`
2 parents dbf31f1 + 8cde275 commit f06c1b8

15 files changed

+164
-51
lines changed

src/tools/clippy/clippy_lints/src/default_constructed_unit_structs.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::{diagnostics::span_lint_and_sugg, match_def_path, paths};
1+
use clippy_utils::{diagnostics::span_lint_and_sugg, is_ty_alias, match_def_path, paths};
22
use hir::{def::Res, ExprKind};
33
use rustc_errors::Applicability;
44
use rustc_hir as hir;
@@ -43,12 +43,23 @@ declare_clippy_lint! {
4343
}
4444
declare_lint_pass!(DefaultConstructedUnitStructs => [DEFAULT_CONSTRUCTED_UNIT_STRUCTS]);
4545

46+
fn is_alias(ty: hir::Ty<'_>) -> bool {
47+
if let hir::TyKind::Path(ref qpath) = ty.kind {
48+
is_ty_alias(qpath)
49+
} else {
50+
false
51+
}
52+
}
53+
4654
impl LateLintPass<'_> for DefaultConstructedUnitStructs {
4755
fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
4856
if_chain!(
4957
// make sure we have a call to `Default::default`
5058
if let hir::ExprKind::Call(fn_expr, &[]) = expr.kind;
51-
if let ExprKind::Path(ref qpath@ hir::QPath::TypeRelative(_,_)) = fn_expr.kind;
59+
if let ExprKind::Path(ref qpath @ hir::QPath::TypeRelative(base, _)) = fn_expr.kind;
60+
// make sure this isn't a type alias:
61+
// `<Foo as Bar>::Assoc` cannot be used as a constructor
62+
if !is_alias(*base);
5263
if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id);
5364
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
5465
// make sure we have a struct with no fields (unit struct)

src/tools/clippy/clippy_lints/src/items_after_test_module.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::{diagnostics::span_lint_and_help, is_in_cfg_test};
1+
use clippy_utils::{diagnostics::span_lint_and_help, is_from_proc_macro, is_in_cfg_test};
22
use rustc_hir::{HirId, ItemId, ItemKind, Mod};
33
use rustc_lint::{LateContext, LateLintPass, LintContext};
44
use rustc_middle::lint::in_external_macro;
@@ -59,6 +59,7 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
5959
if !matches!(item.kind, ItemKind::Mod(_));
6060
if !is_in_cfg_test(cx.tcx, itid.hir_id()); // The item isn't in the testing module itself
6161
if !in_external_macro(cx.sess(), item.span);
62+
if !is_from_proc_macro(cx, item);
6263

6364
then {
6465
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");

src/tools/clippy/clippy_lints/src/let_with_type_underscore.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::source::snippet;
23
use rustc_hir::{Local, TyKind};
34
use rustc_lint::{LateContext, LateLintPass};
45
use rustc_middle::lint::in_external_macro;
@@ -25,14 +26,21 @@ declare_clippy_lint! {
2526
declare_lint_pass!(UnderscoreTyped => [LET_WITH_TYPE_UNDERSCORE]);
2627

2728
impl LateLintPass<'_> for UnderscoreTyped {
28-
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
29+
fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
2930
if_chain! {
3031
if !in_external_macro(cx.tcx.sess, local.span);
3132
if let Some(ty) = local.ty; // Ensure that it has a type defined
3233
if let TyKind::Infer = &ty.kind; // that type is '_'
3334
if local.span.ctxt() == ty.span.ctxt();
3435
then {
35-
span_lint_and_help(cx,
36+
// NOTE: Using `is_from_proc_macro` on `init` will require that it's initialized,
37+
// this doesn't. Alternatively, `WithSearchPat` can be implemented for `Ty`
38+
if snippet(cx, ty.span, "_").trim() != "_" {
39+
return;
40+
}
41+
42+
span_lint_and_help(
43+
cx,
3644
LET_WITH_TYPE_UNDERSCORE,
3745
local.span,
3846
"variable declared with type underscore",

src/tools/clippy/clippy_lints/src/redundant_clone.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ declare_clippy_lint! {
5757
/// ```
5858
#[clippy::version = "1.32.0"]
5959
pub REDUNDANT_CLONE,
60-
perf,
60+
nursery,
6161
"`clone()` of an owned value that is going to be dropped immediately"
6262
}
6363

src/tools/clippy/clippy_utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pub fn is_wild(pat: &Pat<'_>) -> bool {
287287
/// Checks if the given `QPath` belongs to a type alias.
288288
pub fn is_ty_alias(qpath: &QPath<'_>) -> bool {
289289
match *qpath {
290-
QPath::Resolved(_, path) => matches!(path.res, Res::Def(DefKind::TyAlias, ..)),
290+
QPath::Resolved(_, path) => matches!(path.res, Res::Def(DefKind::TyAlias | DefKind::AssocTy, ..)),
291291
QPath::TypeRelative(ty, _) if let TyKind::Path(qpath) = ty.kind => { is_ty_alias(&qpath) },
292292
_ => false,
293293
}

src/tools/clippy/tests/ui/default_constructed_unit_structs.fixed

+22
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ struct EmptyStruct {}
101101
#[non_exhaustive]
102102
struct NonExhaustiveStruct;
103103

104+
mod issue_10755 {
105+
struct Sqlite {}
106+
107+
trait HasArguments<'q> {
108+
type Arguments;
109+
}
110+
111+
impl<'q> HasArguments<'q> for Sqlite {
112+
type Arguments = std::marker::PhantomData<&'q ()>;
113+
}
114+
115+
type SqliteArguments<'q> = <Sqlite as HasArguments<'q>>::Arguments;
116+
117+
fn foo() {
118+
// should not lint
119+
// type alias cannot be used as a constructor
120+
let _ = <Sqlite as HasArguments>::Arguments::default();
121+
122+
let _ = SqliteArguments::default();
123+
}
124+
}
125+
104126
fn main() {
105127
// should lint
106128
let _ = PhantomData::<usize>;

src/tools/clippy/tests/ui/default_constructed_unit_structs.rs

+22
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ struct EmptyStruct {}
101101
#[non_exhaustive]
102102
struct NonExhaustiveStruct;
103103

104+
mod issue_10755 {
105+
struct Sqlite {}
106+
107+
trait HasArguments<'q> {
108+
type Arguments;
109+
}
110+
111+
impl<'q> HasArguments<'q> for Sqlite {
112+
type Arguments = std::marker::PhantomData<&'q ()>;
113+
}
114+
115+
type SqliteArguments<'q> = <Sqlite as HasArguments<'q>>::Arguments;
116+
117+
fn foo() {
118+
// should not lint
119+
// type alias cannot be used as a constructor
120+
let _ = <Sqlite as HasArguments>::Arguments::default();
121+
122+
let _ = SqliteArguments::default();
123+
}
124+
}
125+
104126
fn main() {
105127
// should lint
106128
let _ = PhantomData::<usize>::default();

src/tools/clippy/tests/ui/default_constructed_unit_structs.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ LL | inner: PhantomData::default(),
1313
| ^^^^^^^^^^^ help: remove this call to `default`
1414

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

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

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

3333
error: use of `default` to create a unit struct
34-
--> $DIR/default_constructed_unit_structs.rs:109:23
34+
--> $DIR/default_constructed_unit_structs.rs:131:23
3535
|
3636
LL | let _ = UnitStruct::default();
3737
| ^^^^^^^^^^^ help: remove this call to `default`
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
1+
//@aux-build: proc_macros.rs
12
#![allow(unused)]
23
#![warn(clippy::let_with_type_underscore)]
3-
#![allow(clippy::let_unit_value)]
4+
#![allow(clippy::let_unit_value, clippy::needless_late_init)]
5+
6+
extern crate proc_macros;
47

58
fn func() -> &'static str {
69
""
710
}
811

12+
#[rustfmt::skip]
913
fn main() {
1014
// Will lint
1115
let x: _ = 1;
1216
let _: _ = 2;
1317
let x: _ = func();
18+
let x: _;
19+
x = ();
1420

1521
let x = 1; // Will not lint, Rust infers this to an integer before Clippy
1622
let x = func();
1723
let x: Vec<_> = Vec::<u32>::new();
1824
let x: [_; 1] = [1];
25+
let x : _ = 1;
26+
27+
// Do not lint from procedural macros
28+
proc_macros::with_span! {
29+
span
30+
let x: _ = ();
31+
// Late initialization
32+
let x: _;
33+
x = ();
34+
// Ensure weird formatting will not break it (hopefully)
35+
let x : _ = 1;
36+
let x
37+
: _ = 1;
38+
let x :
39+
_;
40+
x = ();
41+
};
1942
}
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
11
error: variable declared with type underscore
2-
--> $DIR/let_with_type_underscore.rs:11:5
2+
--> $DIR/let_with_type_underscore.rs:15:5
33
|
44
LL | let x: _ = 1;
55
| ^^^^^^^^^^^^^
66
|
77
help: remove the explicit type `_` declaration
8-
--> $DIR/let_with_type_underscore.rs:11:10
8+
--> $DIR/let_with_type_underscore.rs:15:10
99
|
1010
LL | let x: _ = 1;
1111
| ^^^
1212
= note: `-D clippy::let-with-type-underscore` implied by `-D warnings`
1313

1414
error: variable declared with type underscore
15-
--> $DIR/let_with_type_underscore.rs:12:5
15+
--> $DIR/let_with_type_underscore.rs:16:5
1616
|
1717
LL | let _: _ = 2;
1818
| ^^^^^^^^^^^^^
1919
|
2020
help: remove the explicit type `_` declaration
21-
--> $DIR/let_with_type_underscore.rs:12:10
21+
--> $DIR/let_with_type_underscore.rs:16:10
2222
|
2323
LL | let _: _ = 2;
2424
| ^^^
2525

2626
error: variable declared with type underscore
27-
--> $DIR/let_with_type_underscore.rs:13:5
27+
--> $DIR/let_with_type_underscore.rs:17:5
2828
|
2929
LL | let x: _ = func();
3030
| ^^^^^^^^^^^^^^^^^^
3131
|
3232
help: remove the explicit type `_` declaration
33-
--> $DIR/let_with_type_underscore.rs:13:10
33+
--> $DIR/let_with_type_underscore.rs:17:10
3434
|
3535
LL | let x: _ = func();
3636
| ^^^
3737

38-
error: aborting due to 3 previous errors
38+
error: variable declared with type underscore
39+
--> $DIR/let_with_type_underscore.rs:18:5
40+
|
41+
LL | let x: _;
42+
| ^^^^^^^^^
43+
|
44+
help: remove the explicit type `_` declaration
45+
--> $DIR/let_with_type_underscore.rs:18:10
46+
|
47+
LL | let x: _;
48+
| ^^^
49+
50+
error: variable declared with type underscore
51+
--> $DIR/let_with_type_underscore.rs:25:5
52+
|
53+
LL | let x : _ = 1;
54+
| ^^^^^^^^^^^^^^
55+
|
56+
help: remove the explicit type `_` declaration
57+
--> $DIR/let_with_type_underscore.rs:25:10
58+
|
59+
LL | let x : _ = 1;
60+
| ^^^^
61+
62+
error: aborting due to 5 previous errors
3963

src/tools/clippy/tests/ui/redundant_clone.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@run-rustfix
22
// rustfix-only-machine-applicable
33
#![feature(lint_reasons)]
4+
#![warn(clippy::redundant_clone)]
45
#![allow(clippy::drop_non_drop, clippy::implicit_clone, clippy::uninlined_format_args)]
56

67
use std::ffi::OsString;

src/tools/clippy/tests/ui/redundant_clone.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@run-rustfix
22
// rustfix-only-machine-applicable
33
#![feature(lint_reasons)]
4+
#![warn(clippy::redundant_clone)]
45
#![allow(clippy::drop_non_drop, clippy::implicit_clone, clippy::uninlined_format_args)]
56

67
use std::ffi::OsString;

0 commit comments

Comments
 (0)