Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix allow on some statement lints #7282

Merged
merged 1 commit into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clippy_lints/src/loops/needless_collect.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::NEEDLESS_COLLECT;
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::is_type_diagnostic_item;
Expand Down Expand Up @@ -116,9 +116,10 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
// Suggest replacing iter_call with iter_replacement, and removing stmt
let mut span = MultiSpan::from_span(collect_span);
span.push_span_label(iter_call.span, "the iterator could be used here instead".into());
span_lint_and_then(
span_lint_hir_and_then(
cx,
super::NEEDLESS_COLLECT,
init_expr.hir_id,
span,
NEEDLESS_COLLECT_MSG,
|diag| {
Expand Down
4 changes: 3 additions & 1 deletion clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,10 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
if binop.node == BinOpKind::And || binop.node == BinOpKind::Or;
if let Some(sugg) = Sugg::hir_opt(cx, a);
then {
span_lint_and_then(cx,
span_lint_hir_and_then(
cx,
SHORT_CIRCUIT_STATEMENT,
expr.hir_id,
stmt.span,
"boolean short circuit operator in statement may be clearer using an explicit test",
|diag| {
Expand Down
13 changes: 7 additions & 6 deletions clippy_lints/src/no_effect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::has_drop;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
if let StmtKind::Semi(expr) = stmt.kind {
if has_no_effect(cx, expr) {
span_lint(cx, NO_EFFECT, stmt.span, "statement with no effect");
span_lint_hir(cx, NO_EFFECT, expr.hir_id, stmt.span, "statement with no effect");
} else if let Some(reduced) = reduce_expression(cx, expr) {
let mut snippet = String::new();
for e in reduced {
Expand All @@ -106,14 +106,15 @@ impl<'tcx> LateLintPass<'tcx> for NoEffect {
return;
}
}
span_lint_and_sugg(
span_lint_hir_and_then(
cx,
UNNECESSARY_OPERATION,
expr.hir_id,
stmt.span,
"statement can be reduced",
"replace it with",
snippet,
Applicability::MachineApplicable,
|diag| {
diag.span_suggestion(stmt.span, "replace it with", snippet, Applicability::MachineApplicable);
},
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_utils/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub fn span_lint_hir_and_then(
cx: &LateContext<'_>,
lint: &'static Lint,
hir_id: HirId,
sp: Span,
sp: impl Into<MultiSpan>,
msg: &str,
f: impl FnOnce(&mut DiagnosticBuilder<'_>),
) {
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/needless_collect_indirect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BinaryHeap, HashMap, LinkedList, VecDeque};
use std::collections::{BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};

fn main() {
let sample = [1; 5];
Expand Down Expand Up @@ -75,3 +75,9 @@ mod issue7110 {
buffer.len()
}
}

fn allow_test() {
#[allow(clippy::needless_collect)]
let v = [1].iter().collect::<Vec<_>>();
v.into_iter().collect::<HashSet<_>>();
}
3 changes: 3 additions & 0 deletions tests/ui/no_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ fn main() {
let s: String = "foo".into();
FooString { s: s };

#[allow(clippy::no_effect)]
0;

// Do not warn
get_number();
unsafe { unsafe_fn() };
Expand Down