Skip to content

Use span_suggestion_with_applicability instead of span_suggestion #3191

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

Merged
merged 8 commits into from
Sep 23, 2018
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
14 changes: 11 additions & 3 deletions clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use if_chain::if_chain;
use crate::syntax::ast;
use crate::rustc_errors::Applicability;

/// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
/// patterns.
Expand Down Expand Up @@ -78,7 +79,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
let r = &sugg::Sugg::hir(cx, rhs, "..");
let long =
format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r));
db.span_suggestion(
db.span_suggestion_with_applicability(
expr.span,
&format!(
"Did you mean {} = {} {} {} or {}? Consider replacing it with",
Expand All @@ -89,8 +90,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
long
),
format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
Applicability::MachineApplicable,
);
db.span_suggestion_with_applicability(
expr.span,
"or",
long,
Applicability::MachineApplicable, // snippet
);
db.span_suggestion(expr.span, "or", long);
}
},
);
Expand Down Expand Up @@ -172,10 +179,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
if let (Some(snip_a), Some(snip_r)) =
(snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
{
db.span_suggestion(
db.span_suggestion_with_applicability(
expr.span,
"replace it with",
format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
Applicability::MachineApplicable,
);
}
},
Expand Down
8 changes: 7 additions & 1 deletion clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::rustc::ty::{self, TyCtxt};
use semver::Version;
use crate::syntax::ast::{AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
use crate::syntax::source_map::Span;
use crate::rustc_errors::Applicability;

/// **What it does:** Checks for items annotated with `#[inline(always)]`,
/// unless the annotated function is empty or simply panics.
Expand Down Expand Up @@ -203,7 +204,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
"useless lint attribute",
|db| {
sugg = sugg.replacen("#[", "#![", 1);
db.span_suggestion(line_span, "if you just forgot a `!`, use", sugg);
db.span_suggestion_with_applicability(
line_span,
"if you just forgot a `!`, use",
sugg,
Applicability::MachineApplicable,
);
},
);
}
Expand Down
8 changes: 7 additions & 1 deletion clippy_lints/src/bit_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::syntax::source_map::Span;
use crate::utils::{span_lint, span_lint_and_then};
use crate::utils::sugg::Sugg;
use crate::consts::{constant, Constant};
use crate::rustc_errors::Applicability;

/// **What it does:** Checks for incompatible bit masks in comparisons.
///
Expand Down Expand Up @@ -138,7 +139,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
"bit mask could be simplified with a call to `trailing_zeros`",
|db| {
let sugg = Sugg::hir(cx, left1, "...").maybe_par();
db.span_suggestion(e.span, "try", format!("{}.trailing_zeros() >= {}", sugg, n.count_ones()));
db.span_suggestion_with_applicability(
e.span,
"try",
format!("{}.trailing_zeros() >= {}", sugg, n.count_ones()),
Applicability::MaybeIncorrect,
);
});
}
}
Expand Down
17 changes: 15 additions & 2 deletions clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::syntax::ast::{LitKind, NodeId, DUMMY_NODE_ID};
use crate::syntax::source_map::{dummy_spanned, Span, DUMMY_SP};
use crate::rustc_data_structures::thin_vec::ThinVec;
use crate::utils::{in_macro, paths, match_type, snippet_opt, span_lint_and_then, SpanlessEq, get_trait_def_id, implements_trait};
use crate::rustc_errors::Applicability;

/// **What it does:** Checks for boolean expressions that can be written more
/// concisely.
Expand Down Expand Up @@ -390,10 +391,13 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
"this expression can be optimized out by applying boolean operations to the \
outer expression",
);
db.span_suggestion(
db.span_suggestion_with_applicability(
e.span,
"it would look like the following",
suggest(self.cx, suggestion, &h2q.terminals).0,
// nonminimal_bool can produce minimal but
// not human readable expressions (#3141)
Applicability::Unspecified,
);
},
);
Expand All @@ -416,7 +420,16 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
NONMINIMAL_BOOL,
e.span,
"this boolean expression can be simplified",
|db| { db.span_suggestions(e.span, "try", suggestions); },
|db| {
db.span_suggestions_with_applicability(
e.span,
"try",
suggestions,
// nonminimal_bool can produce minimal but
// not human readable expressions (#3141)
Applicability::Unspecified,
);
},
);
};
if improvements.is_empty() {
Expand Down
16 changes: 11 additions & 5 deletions clippy_lints/src/collapsible_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::syntax::ast;

use crate::utils::{in_macro, snippet_block, span_lint_and_sugg, span_lint_and_then};
use crate::utils::sugg::Sugg;
use crate::rustc_errors::Applicability;

/// **What it does:** Checks for nested `if` statements which can be collapsed
/// by `&&`-combining their conditions and for `else { if ... }` expressions
Expand Down Expand Up @@ -133,11 +134,16 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: &
span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this if statement can be collapsed", |db| {
let lhs = Sugg::ast(cx, check, "..");
let rhs = Sugg::ast(cx, check_inner, "..");
db.span_suggestion(expr.span,
"try",
format!("if {} {}",
lhs.and(&rhs),
snippet_block(cx, content.span, "..")));
db.span_suggestion_with_applicability(
expr.span,
"try",
format!(
"if {} {}",
lhs.and(&rhs),
snippet_block(cx, content.span, ".."),
),
Applicability::MachineApplicable, // snippet
);
});
}
}
Expand Down
8 changes: 7 additions & 1 deletion clippy_lints/src/const_static_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::syntax::ast::*;
use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::utils::{in_macro, snippet, span_lint_and_then};
use crate::rustc_errors::Applicability;

/// **What it does:** Checks for constants with an explicit `'static` lifetime.
///
Expand Down Expand Up @@ -60,7 +61,12 @@ impl StaticConst {
lifetime.ident.span,
"Constants have by default a `'static` lifetime",
|db| {
db.span_suggestion(ty.span, "consider removing `'static`", sugg);
db.span_suggestion_with_applicability(
ty.span,
"consider removing `'static`",
sugg,
Applicability::MachineApplicable, //snippet
);
},
);
}
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ fn lint_match_arms(cx: &LateContext<'_, '_>, expr: &Expr) {
|db| {
db.span_note(i.body.span, "same as this");

// Note: this does not use `span_suggestion` on purpose: there is no clean way
// Note: this does not use `span_suggestion_with_applicability` on purpose:
// there is no clean way
// to remove the other arm. Building a span and suggest to replace it to ""
// makes an even more confusing error message. Also in order not to make up a
// span for the whole pattern, the suggestion is only shown when there is only
Expand Down
15 changes: 13 additions & 2 deletions clippy_lints/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use if_chain::if_chain;
use crate::syntax::source_map::Span;
use crate::utils::SpanlessEq;
use crate::utils::{get_item_name, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty};
use crate::rustc_errors::Applicability;

/// **What it does:** Checks for uses of `contains_key` + `insert` on `HashMap`
/// or `BTreeMap`.
Expand Down Expand Up @@ -139,14 +140,24 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
snippet(self.cx, params[1].span, ".."),
snippet(self.cx, params[2].span, ".."));

db.span_suggestion(self.span, "consider using", help);
db.span_suggestion_with_applicability(
self.span,
"consider using",
help,
Applicability::MachineApplicable, // snippet
);
}
else {
let help = format!("{}.entry({})",
snippet(self.cx, self.map.span, "map"),
snippet(self.cx, params[1].span, ".."));

db.span_suggestion(self.span, "consider using", help);
db.span_suggestion_with_applicability(
self.span,
"consider using",
help,
Applicability::MachineApplicable, // snippet
);
}
});
}
Expand Down
29 changes: 25 additions & 4 deletions clippy_lints/src/eq_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::rustc::hir::*;
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::utils::{in_macro, implements_trait, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq};
use crate::rustc_errors::Applicability;

/// **What it does:** Checks for equal operands to comparison, logical and
/// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
Expand Down Expand Up @@ -113,7 +114,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
} else if lcpy && !rcpy && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()]) {
span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
let lsnip = snippet(cx, l.span, "...").to_string();
db.span_suggestion(left.span, "use the left value directly", lsnip);
db.span_suggestion_with_applicability(
left.span,
"use the left value directly",
lsnip,
Applicability::MachineApplicable, // snippet
);
})
} else if !lcpy && rcpy && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()]) {
span_lint_and_then(
Expand All @@ -123,7 +129,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
"needlessly taken reference of right operand",
|db| {
let rsnip = snippet(cx, r.span, "...").to_string();
db.span_suggestion(right.span, "use the right value directly", rsnip);
db.span_suggestion_with_applicability(
right.span,
"use the right value directly",
rsnip,
Applicability::MachineApplicable, // snippet
);
},
)
}
Expand All @@ -135,7 +146,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
if (requires_ref || lcpy) && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()]) {
span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
let lsnip = snippet(cx, l.span, "...").to_string();
db.span_suggestion(left.span, "use the left value directly", lsnip);
db.span_suggestion_with_applicability(
left.span,
"use the left value directly",
lsnip,
Applicability::MachineApplicable, // snippet
);
})
}
},
Expand All @@ -146,7 +162,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
if (requires_ref || rcpy) && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()]) {
span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
let rsnip = snippet(cx, r.span, "...").to_string();
db.span_suggestion(right.span, "use the right value directly", rsnip);
db.span_suggestion_with_applicability(
right.span,
"use the right value directly",
rsnip,
Applicability::MachineApplicable, // snippet
);
})
}
},
Expand Down
8 changes: 7 additions & 1 deletion clippy_lints/src/eta_reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc::ty;
use crate::rustc::hir::*;
use crate::utils::{is_adjusted, iter_input_pats, snippet_opt, span_lint_and_then};
use crate::rustc_errors::Applicability;

#[allow(missing_copy_implementations)]
pub struct EtaPass;
Expand Down Expand Up @@ -96,7 +97,12 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) {
}
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found", |db| {
if let Some(snippet) = snippet_opt(cx, caller.span) {
db.span_suggestion(expr.span, "remove closure as shown", snippet);
db.span_suggestion_with_applicability(
expr.span,
"remove closure as shown",
snippet,
Applicability::MachineApplicable,
);
}
});
}
Expand Down
15 changes: 13 additions & 2 deletions clippy_lints/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::syntax::ast::LitKind;
use crate::syntax_pos::Span;
use crate::utils::paths;
use crate::utils::{in_macro, is_expn_of, last_path_segment, match_def_path, match_type, opt_def_id, resolve_node, snippet, span_lint_and_then, walk_ptrs_ty};
use crate::rustc_errors::Applicability;

/// **What it does:** Checks for the use of `format!("string literal with no
/// argument")` and `format!("{}", foo)` where `foo` is a string.
Expand Down Expand Up @@ -60,7 +61,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
then {
let sugg = format!("{}.to_string()", snippet(cx, format_arg, "<arg>").into_owned());
span_lint_and_then(cx, USELESS_FORMAT, span, "useless use of `format!`", |db| {
db.span_suggestion(expr.span, "consider using .to_string()", sugg);
db.span_suggestion_with_applicability(
expr.span,
"consider using .to_string()",
sugg,
Applicability::MachineApplicable,
);
});
}
}
Expand All @@ -70,7 +76,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if tup.is_empty() {
let sugg = format!("{}.to_string()", snippet(cx, expr.span, "<expr>").into_owned());
span_lint_and_then(cx, USELESS_FORMAT, span, "useless use of `format!`", |db| {
db.span_suggestion(span, "consider using .to_string()", sugg);
db.span_suggestion_with_applicability(
span,
"consider using .to_string()",
sugg,
Applicability::MachineApplicable, // snippet
);
});
}
},
Expand Down
Loading