Skip to content

Commit ff89336

Browse files
committed
Mark let_underscore_drop as uplifted
1 parent 4f142aa commit ff89336

15 files changed

+121
-253
lines changed

clippy_lints/src/let_underscore.rs

Lines changed: 42 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::{is_must_use_ty, is_type_diagnostic_item, match_type};
33
use clippy_utils::{is_must_use_func_call, paths};
4-
use if_chain::if_chain;
54
use rustc_hir::{Local, PatKind};
65
use rustc_lint::{LateContext, LateLintPass};
76
use rustc_middle::lint::in_external_macro;
@@ -60,45 +59,7 @@ declare_clippy_lint! {
6059
"non-binding let on a synchronization lock"
6160
}
6261

63-
declare_clippy_lint! {
64-
/// ### What it does
65-
/// Checks for `let _ = <expr>`
66-
/// where expr has a type that implements `Drop`
67-
///
68-
/// ### Why is this bad?
69-
/// This statement immediately drops the initializer
70-
/// expression instead of extending its lifetime to the end of the scope, which
71-
/// is often not intended. To extend the expression's lifetime to the end of the
72-
/// scope, use an underscore-prefixed name instead (i.e. _var). If you want to
73-
/// explicitly drop the expression, `std::mem::drop` conveys your intention
74-
/// better and is less error-prone.
75-
///
76-
/// ### Example
77-
/// ```rust
78-
/// # struct DroppableItem;
79-
/// {
80-
/// let _ = DroppableItem;
81-
/// // ^ dropped here
82-
/// /* more code */
83-
/// }
84-
/// ```
85-
///
86-
/// Use instead:
87-
/// ```rust
88-
/// # struct DroppableItem;
89-
/// {
90-
/// let _droppable = DroppableItem;
91-
/// /* more code */
92-
/// // dropped at end of scope
93-
/// }
94-
/// ```
95-
#[clippy::version = "1.50.0"]
96-
pub LET_UNDERSCORE_DROP,
97-
pedantic,
98-
"non-binding let on a type that implements `Drop`"
99-
}
100-
101-
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]);
62+
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK]);
10263

10364
const SYNC_GUARD_SYMS: [Symbol; 3] = [sym::MutexGuard, sym::RwLockReadGuard, sym::RwLockWriteGuard];
10465

@@ -110,64 +71,49 @@ const SYNC_GUARD_PATHS: [&[&str]; 3] = [
11071

11172
impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
11273
fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
113-
if in_external_macro(cx.tcx.sess, local.span) {
114-
return;
115-
}
74+
if !in_external_macro(cx.tcx.sess, local.span)
75+
&& let PatKind::Wild = local.pat.kind
76+
&& let Some(init) = local.init
77+
{
78+
let init_ty = cx.typeck_results().expr_ty(init);
79+
let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
80+
GenericArgKind::Type(inner_ty) => {
81+
SYNC_GUARD_SYMS
82+
.iter()
83+
.any(|&sym| is_type_diagnostic_item(cx, inner_ty, sym))
84+
|| SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
85+
},
11686

117-
if_chain! {
118-
if let PatKind::Wild = local.pat.kind;
119-
if let Some(init) = local.init;
120-
then {
121-
let init_ty = cx.typeck_results().expr_ty(init);
122-
let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
123-
GenericArgKind::Type(inner_ty) => {
124-
SYNC_GUARD_SYMS
125-
.iter()
126-
.any(|&sym| is_type_diagnostic_item(cx, inner_ty, sym))
127-
|| SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
128-
},
129-
130-
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
131-
});
132-
if contains_sync_guard {
133-
span_lint_and_help(
134-
cx,
135-
LET_UNDERSCORE_LOCK,
136-
local.span,
137-
"non-binding let on a synchronization lock",
138-
None,
139-
"consider using an underscore-prefixed named \
140-
binding or dropping explicitly with `std::mem::drop`",
141-
);
142-
} else if init_ty.needs_drop(cx.tcx, cx.param_env) {
143-
span_lint_and_help(
144-
cx,
145-
LET_UNDERSCORE_DROP,
146-
local.span,
147-
"non-binding `let` on a type that implements `Drop`",
148-
None,
149-
"consider using an underscore-prefixed named \
87+
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
88+
});
89+
if contains_sync_guard {
90+
span_lint_and_help(
91+
cx,
92+
LET_UNDERSCORE_LOCK,
93+
local.span,
94+
"non-binding let on a synchronization lock",
95+
None,
96+
"consider using an underscore-prefixed named \
15097
binding or dropping explicitly with `std::mem::drop`",
151-
);
152-
} else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
153-
span_lint_and_help(
154-
cx,
155-
LET_UNDERSCORE_MUST_USE,
156-
local.span,
157-
"non-binding let on an expression with `#[must_use]` type",
158-
None,
159-
"consider explicitly using expression value",
160-
);
161-
} else if is_must_use_func_call(cx, init) {
162-
span_lint_and_help(
163-
cx,
164-
LET_UNDERSCORE_MUST_USE,
165-
local.span,
166-
"non-binding let on a result of a `#[must_use]` function",
167-
None,
168-
"consider explicitly using function result",
169-
);
170-
}
98+
);
99+
} else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
100+
span_lint_and_help(
101+
cx,
102+
LET_UNDERSCORE_MUST_USE,
103+
local.span,
104+
"non-binding let on an expression with `#[must_use]` type",
105+
None,
106+
"consider explicitly using expression value",
107+
);
108+
} else if is_must_use_func_call(cx, init) {
109+
span_lint_and_help(
110+
cx,
111+
LET_UNDERSCORE_MUST_USE,
112+
local.span,
113+
"non-binding let on a result of a `#[must_use]` function",
114+
None,
115+
"consider explicitly using function result",
116+
);
171117
}
172118
}
173119
}

clippy_lints/src/lib.register_lints.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ store.register_lints(&[
214214
len_zero::LEN_WITHOUT_IS_EMPTY,
215215
len_zero::LEN_ZERO,
216216
let_if_seq::USELESS_LET_IF_SEQ,
217-
let_underscore::LET_UNDERSCORE_DROP,
218217
let_underscore::LET_UNDERSCORE_LOCK,
219218
let_underscore::LET_UNDERSCORE_MUST_USE,
220219
lifetimes::EXTRA_UNUSED_LIFETIMES,

clippy_lints/src/lib.register_pedantic.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
3939
LintId::of(items_after_statements::ITEMS_AFTER_STATEMENTS),
4040
LintId::of(iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR),
4141
LintId::of(large_stack_arrays::LARGE_STACK_ARRAYS),
42-
LintId::of(let_underscore::LET_UNDERSCORE_DROP),
4342
LintId::of(literal_representation::LARGE_DIGIT_GROUPS),
4443
LintId::of(literal_representation::UNREADABLE_LITERAL),
4544
LintId::of(loops::EXPLICIT_INTO_ITER_LOOP),

clippy_lints/src/renamed_lints.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[
1111
("clippy::disallowed_method", "clippy::disallowed_methods"),
1212
("clippy::disallowed_type", "clippy::disallowed_types"),
1313
("clippy::eval_order_dependence", "clippy::mixed_read_write_in_expression"),
14-
("clippy::for_loop_over_option", "for_loops_over_fallibles"),
15-
("clippy::for_loop_over_result", "for_loops_over_fallibles"),
1614
("clippy::identity_conversion", "clippy::useless_conversion"),
1715
("clippy::if_let_some_result", "clippy::match_result_ok"),
1816
("clippy::logic_bug", "clippy::overly_complex_bool_expr"),
@@ -31,10 +29,13 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[
3129
("clippy::to_string_in_display", "clippy::recursive_format_impl"),
3230
("clippy::zero_width_space", "clippy::invisible_characters"),
3331
("clippy::drop_bounds", "drop_bounds"),
32+
("clippy::for_loop_over_option", "for_loops_over_fallibles"),
33+
("clippy::for_loop_over_result", "for_loops_over_fallibles"),
3434
("clippy::for_loops_over_fallibles", "for_loops_over_fallibles"),
3535
("clippy::into_iter_on_array", "array_into_iter"),
3636
("clippy::invalid_atomic_ordering", "invalid_atomic_ordering"),
3737
("clippy::invalid_ref", "invalid_value"),
38+
("clippy::let_underscore_drop", "let_underscore_drop"),
3839
("clippy::mem_discriminant_non_enum", "enum_intrinsics_non_enums"),
3940
("clippy::panic_params", "non_fmt_panics"),
4041
("clippy::positional_named_format_parameters", "named_arguments_used_positionally"),

lintcheck/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ fn main() {
678678
.unwrap();
679679

680680
let server = config.recursive.then(|| {
681-
fs::remove_dir_all("target/lintcheck/shared_target_dir/recursive").unwrap_or_default();
681+
let _ = fs::remove_dir_all("target/lintcheck/shared_target_dir/recursive");
682682

683683
LintcheckServer::spawn(recursive_options)
684684
});

src/docs.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ docs! {
247247
"len_without_is_empty",
248248
"len_zero",
249249
"let_and_return",
250-
"let_underscore_drop",
251250
"let_underscore_lock",
252251
"let_underscore_must_use",
253252
"let_unit_value",

src/docs/let_underscore_drop.txt

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/ui/let_underscore_drop.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/ui/let_underscore_drop.stderr

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/ui/map_flatten_fixable.fixed

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// run-rustfix
22

33
#![warn(clippy::all, clippy::pedantic)]
4-
#![allow(clippy::let_underscore_drop)]
54
#![allow(clippy::missing_docs_in_private_items)]
65
#![allow(clippy::map_identity)]
76
#![allow(clippy::redundant_closure)]

tests/ui/map_flatten_fixable.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// run-rustfix
22

33
#![warn(clippy::all, clippy::pedantic)]
4-
#![allow(clippy::let_underscore_drop)]
54
#![allow(clippy::missing_docs_in_private_items)]
65
#![allow(clippy::map_identity)]
76
#![allow(clippy::redundant_closure)]

tests/ui/map_flatten_fixable.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
error: called `map(..).flatten()` on `Iterator`
2-
--> $DIR/map_flatten_fixable.rs:18:47
2+
--> $DIR/map_flatten_fixable.rs:17:47
33
|
44
LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id).flatten().collect();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(option_id)`
66
|
77
= note: `-D clippy::map-flatten` implied by `-D warnings`
88

99
error: called `map(..).flatten()` on `Iterator`
10-
--> $DIR/map_flatten_fixable.rs:19:47
10+
--> $DIR/map_flatten_fixable.rs:18:47
1111
|
1212
LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id_ref).flatten().collect();
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(option_id_ref)`
1414

1515
error: called `map(..).flatten()` on `Iterator`
16-
--> $DIR/map_flatten_fixable.rs:20:47
16+
--> $DIR/map_flatten_fixable.rs:19:47
1717
|
1818
LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id_closure).flatten().collect();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(option_id_closure)`
2020

2121
error: called `map(..).flatten()` on `Iterator`
22-
--> $DIR/map_flatten_fixable.rs:21:47
22+
--> $DIR/map_flatten_fixable.rs:20:47
2323
|
2424
LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| x.checked_add(1)).flatten().collect();
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(|x| x.checked_add(1))`
2626

2727
error: called `map(..).flatten()` on `Iterator`
28-
--> $DIR/map_flatten_fixable.rs:24:47
28+
--> $DIR/map_flatten_fixable.rs:23:47
2929
|
3030
LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| 0..x).flatten().collect();
3131
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `flat_map` and remove the `.flatten()`: `flat_map(|x| 0..x)`
3232

3333
error: called `map(..).flatten()` on `Option`
34-
--> $DIR/map_flatten_fixable.rs:27:40
34+
--> $DIR/map_flatten_fixable.rs:26:40
3535
|
3636
LL | let _: Option<_> = (Some(Some(1))).map(|x| x).flatten();
3737
| ^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `and_then` and remove the `.flatten()`: `and_then(|x| x)`
3838

3939
error: called `map(..).flatten()` on `Result`
40-
--> $DIR/map_flatten_fixable.rs:30:42
40+
--> $DIR/map_flatten_fixable.rs:29:42
4141
|
4242
LL | let _: Result<_, &str> = (Ok(Ok(1))).map(|x| x).flatten();
4343
| ^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `and_then` and remove the `.flatten()`: `and_then(|x| x)`
4444

4545
error: called `map(..).flatten()` on `Iterator`
46-
--> $DIR/map_flatten_fixable.rs:39:10
46+
--> $DIR/map_flatten_fixable.rs:38:10
4747
|
4848
LL | .map(|n| match n {
4949
| __________^
@@ -72,7 +72,7 @@ LL ~ });
7272
|
7373

7474
error: called `map(..).flatten()` on `Option`
75-
--> $DIR/map_flatten_fixable.rs:59:10
75+
--> $DIR/map_flatten_fixable.rs:58:10
7676
|
7777
LL | .map(|_| {
7878
| __________^

0 commit comments

Comments
 (0)