Skip to content

Commit 8066f83

Browse files
committed
Auto merge of rust-lang#7282 - camsteffen:lint-stmt-expr, r=flip1995
Fix allow on some statement lints changelog: Fix `#[allow(..)]` over statements for [`needless_collect`], [`short_circuit_statement`] and [`unnecessary_operation`] Fixes rust-lang#7171 Fixes rust-lang#7202
2 parents 9c4651f + f3e77a4 commit 8066f83

File tree

6 files changed

+24
-11
lines changed

6 files changed

+24
-11
lines changed

clippy_lints/src/loops/needless_collect.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::NEEDLESS_COLLECT;
2-
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
33
use clippy_utils::source::{snippet, snippet_with_applicability};
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::ty::is_type_diagnostic_item;
@@ -116,9 +116,10 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
116116
// Suggest replacing iter_call with iter_replacement, and removing stmt
117117
let mut span = MultiSpan::from_span(collect_span);
118118
span.push_span_label(iter_call.span, "the iterator could be used here instead".into());
119-
span_lint_and_then(
119+
span_lint_hir_and_then(
120120
cx,
121121
super::NEEDLESS_COLLECT,
122+
init_expr.hir_id,
122123
span,
123124
NEEDLESS_COLLECT_MSG,
124125
|diag| {

clippy_lints/src/misc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,10 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
355355
if binop.node == BinOpKind::And || binop.node == BinOpKind::Or;
356356
if let Some(sugg) = Sugg::hir_opt(cx, a);
357357
then {
358-
span_lint_and_then(cx,
358+
span_lint_hir_and_then(
359+
cx,
359360
SHORT_CIRCUIT_STATEMENT,
361+
expr.hir_id,
360362
stmt.span,
361363
"boolean short circuit operator in statement may be clearer using an explicit test",
362364
|diag| {

clippy_lints/src/no_effect.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
1+
use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
22
use clippy_utils::source::snippet_opt;
33
use clippy_utils::ty::has_drop;
44
use rustc_errors::Applicability;
@@ -92,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
9292
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
9393
if let StmtKind::Semi(expr) = stmt.kind {
9494
if has_no_effect(cx, expr) {
95-
span_lint(cx, NO_EFFECT, stmt.span, "statement with no effect");
95+
span_lint_hir(cx, NO_EFFECT, expr.hir_id, stmt.span, "statement with no effect");
9696
} else if let Some(reduced) = reduce_expression(cx, expr) {
9797
let mut snippet = String::new();
9898
for e in reduced {
@@ -106,14 +106,15 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
106106
return;
107107
}
108108
}
109-
span_lint_and_sugg(
109+
span_lint_hir_and_then(
110110
cx,
111111
UNNECESSARY_OPERATION,
112+
expr.hir_id,
112113
stmt.span,
113114
"statement can be reduced",
114-
"replace it with",
115-
snippet,
116-
Applicability::MachineApplicable,
115+
|diag| {
116+
diag.span_suggestion(stmt.span, "replace it with", snippet, Applicability::MachineApplicable);
117+
},
117118
);
118119
}
119120
}

clippy_utils/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn span_lint_hir_and_then(
167167
cx: &LateContext<'_>,
168168
lint: &'static Lint,
169169
hir_id: HirId,
170-
sp: Span,
170+
sp: impl Into<MultiSpan>,
171171
msg: &str,
172172
f: impl FnOnce(&mut DiagnosticBuilder<'_>),
173173
) {

tests/ui/needless_collect_indirect.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{BinaryHeap, HashMap, LinkedList, VecDeque};
1+
use std::collections::{BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
22

33
fn main() {
44
let sample = [1; 5];
@@ -75,3 +75,9 @@ mod issue7110 {
7575
buffer.len()
7676
}
7777
}
78+
79+
fn allow_test() {
80+
#[allow(clippy::needless_collect)]
81+
let v = [1].iter().collect::<Vec<_>>();
82+
v.into_iter().collect::<HashSet<_>>();
83+
}

tests/ui/no_effect.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ fn main() {
9191
let s: String = "foo".into();
9292
FooString { s: s };
9393

94+
#[allow(clippy::no_effect)]
95+
0;
96+
9497
// Do not warn
9598
get_number();
9699
unsafe { unsafe_fn() };

0 commit comments

Comments
 (0)