Skip to content

Commit

Permalink
Merge pull request #3459 from flip1995/sugg_appl
Browse files Browse the repository at this point in the history
Add Applicability to suggestion lints: Take 2
  • Loading branch information
oli-obk authored Nov 27, 2018
2 parents dec389a + 87e72a5 commit b2601be
Show file tree
Hide file tree
Showing 33 changed files with 545 additions and 295 deletions.
1 change: 1 addition & 0 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ impl EarlyLintPass for CfgAttrPass {
"`cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes",
"use",
format!("{}rustfmt::skip]", attr_style),
Applicability::MachineApplicable,
);
}
}
Expand Down
31 changes: 19 additions & 12 deletions clippy_lints/src/bytecount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@

use crate::rustc::hir::*;
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use if_chain::if_chain;
use crate::rustc::ty;
use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc_errors::Applicability;
use crate::syntax::ast::{Name, UintTy};
use crate::utils::{contains_name, get_pat_name, match_type, paths, single_segment_path, snippet, span_lint_and_sugg,
walk_ptrs_ty};
use crate::utils::{
contains_name, get_pat_name, match_type, paths, single_segment_path, snippet_with_applicability,
span_lint_and_sugg, walk_ptrs_ty,
};
use if_chain::if_chain;

/// **What it does:** Checks for naive byte counts
///
Expand Down Expand Up @@ -89,14 +92,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount {
} else {
&filter_args[0]
};
span_lint_and_sugg(cx,
NAIVE_BYTECOUNT,
expr.span,
"You appear to be counting bytes the naive way",
"Consider using the bytecount crate",
format!("bytecount::count({}, {})",
snippet(cx, haystack.span, ".."),
snippet(cx, needle.span, "..")));
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
NAIVE_BYTECOUNT,
expr.span,
"You appear to be counting bytes the naive way",
"Consider using the bytecount crate",
format!("bytecount::count({}, {})",
snippet_with_applicability(cx, haystack.span, "..", &mut applicability),
snippet_with_applicability(cx, needle.span, "..", &mut applicability)),
applicability,
);
}
};
}
Expand Down
18 changes: 11 additions & 7 deletions clippy_lints/src/collapsible_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::rustc::{declare_tool_lint, lint_array};
use if_chain::if_chain;
use crate::syntax::ast;

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

Expand Down Expand Up @@ -128,12 +128,16 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
then {
match else_.node {
ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {
span_lint_and_sugg(cx,
COLLAPSIBLE_IF,
block.span,
"this `else { if .. }` block can be collapsed",
"try",
snippet_block(cx, else_.span, "..").into_owned());
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
COLLAPSIBLE_IF,
block.span,
"this `else { if .. }` block can be collapsed",
"try",
snippet_block_with_applicability(cx, else_.span, "..", &mut applicability).into_owned(),
applicability,
);
}
_ => (),
}
Expand Down
7 changes: 5 additions & 2 deletions clippy_lints/src/default_trait_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

use crate::rustc::hir::*;
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::ty::TyKind;
use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc_errors::Applicability;
use if_chain::if_chain;
use crate::rustc::ty::TyKind;

use crate::utils::{any_parent_is_automatically_derived, match_def_path, opt_def_id, paths, span_lint_and_sugg};

Expand Down Expand Up @@ -80,7 +81,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
expr.span,
&format!("Calling {} is more clear than this expression", replacement),
"try",
replacement);
replacement,
Applicability::Unspecified, // First resolve the TODO above
);
}
},
QPath::TypeRelative(..) => {},
Expand Down
20 changes: 14 additions & 6 deletions clippy_lints/src/double_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
use crate::rustc::hir::*;
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc_errors::Applicability;
use crate::syntax::source_map::Span;

use crate::utils::{snippet, span_lint_and_sugg, SpanlessEq};
use crate::utils::{snippet_with_applicability, span_lint_and_sugg, SpanlessEq};

/// **What it does:** Checks for double comparions that could be simpified to a single expression.
///
Expand Down Expand Up @@ -70,12 +71,19 @@ impl<'a, 'tcx> Pass {
}
macro_rules! lint_double_comparison {
($op:tt) => {{
let lhs_str = snippet(cx, llhs.span, "");
let rhs_str = snippet(cx, lrhs.span, "");
let mut applicability = Applicability::MachineApplicable;
let lhs_str = snippet_with_applicability(cx, llhs.span, "", &mut applicability);
let rhs_str = snippet_with_applicability(cx, lrhs.span, "", &mut applicability);
let sugg = format!("{} {} {}", lhs_str, stringify!($op), rhs_str);
span_lint_and_sugg(cx, DOUBLE_COMPARISONS, span,
"This binary expression can be simplified",
"try", sugg);
span_lint_and_sugg(
cx,
DOUBLE_COMPARISONS,
span,
"This binary expression can be simplified",
"try",
sugg,
applicability,
);
}}
}
match (op, lkind, rkind) {
Expand Down
9 changes: 6 additions & 3 deletions clippy_lints/src/duration_subsec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
use crate::rustc::hir::*;
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use if_chain::if_chain;
use crate::rustc_errors::Applicability;
use crate::syntax::source_map::Spanned;
use if_chain::if_chain;

use crate::consts::{constant, Constant};
use crate::utils::paths;
use crate::utils::{match_type, snippet, span_lint_and_sugg, walk_ptrs_ty};
use crate::utils::{match_type, snippet_with_applicability, span_lint_and_sugg, walk_ptrs_ty};

/// **What it does:** Checks for calculation of subsecond microseconds or milliseconds
/// from other `Duration` methods.
Expand Down Expand Up @@ -60,13 +61,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DurationSubsec {
("subsec_nanos", 1_000) => "subsec_micros",
_ => return,
};
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
DURATION_SUBSEC,
expr.span,
&format!("Calling `{}()` is more concise than this calculation", suggested_fn),
"try",
format!("{}.{}()", snippet(cx, args[0].span, "_"), suggested_fn),
format!("{}.{}()", snippet_with_applicability(cx, args[0].span, "_", &mut applicability), suggested_fn),
applicability,
);
}
}
Expand Down
5 changes: 2 additions & 3 deletions clippy_lints/src/else_if_without_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass, in_ex
use crate::rustc::{declare_tool_lint, lint_array};
use crate::syntax::ast::*;

use crate::utils::span_lint_and_sugg;
use crate::utils::span_help_and_lint;

/// **What it does:** Checks for usage of if expressions with an `else if` branch,
/// but without a final `else` branch.
Expand Down Expand Up @@ -66,13 +66,12 @@ impl EarlyLintPass for ElseIfWithoutElse {

while let ExprKind::If(_, _, Some(ref els)) = item.node {
if let ExprKind::If(_, _, None) = els.node {
span_lint_and_sugg(
span_help_and_lint(
cx,
ELSE_IF_WITHOUT_ELSE,
els.span,
"if expression with an `else if`, but without a final `else`",
"add an `else` block here",
String::new()
);
}

Expand Down
10 changes: 6 additions & 4 deletions clippy_lints/src/excessive_precision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@

use crate::rustc::hir;
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::ty::TyKind;
use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc_errors::Applicability;
use crate::syntax::ast::*;
use crate::syntax_pos::symbol::Symbol;
use crate::utils::span_lint_and_sugg;
use if_chain::if_chain;
use crate::rustc::ty::TyKind;
use std::f32;
use std::f64;
use std::fmt;
use crate::syntax::ast::*;
use crate::syntax_pos::symbol::Symbol;
use crate::utils::span_lint_and_sugg;

/// **What it does:** Checks for float literals with a precision greater
/// than that supported by the underlying type
Expand Down Expand Up @@ -68,6 +69,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
"float has excessive precision",
"consider changing the type or truncating it to",
sugg,
Applicability::MachineApplicable,
);
}
}
Expand Down
11 changes: 7 additions & 4 deletions clippy_lints/src/infallible_destructuring_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
// except according to those terms.


use super::utils::{get_arg_name, match_var, remove_blocks, snippet, span_lint_and_sugg};
use super::utils::{get_arg_name, match_var, remove_blocks, snippet_with_applicability, span_lint_and_sugg};
use crate::rustc::hir::*;
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc_errors::Applicability;
use if_chain::if_chain;

/// **What it does:** Checks for matches being used to destructure a single-variant enum
Expand Down Expand Up @@ -71,6 +72,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if match_var(body, arg);

then {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
INFALLIBLE_DESTRUCTURING_MATCH,
Expand All @@ -80,10 +82,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
"try this",
format!(
"let {}({}) = {};",
snippet(cx, variant_name.span, ".."),
snippet(cx, local.pat.span, ".."),
snippet(cx, target.span, ".."),
snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
snippet_with_applicability(cx, target.span, "..", &mut applicability),
),
applicability,
);
}
}
Expand Down
19 changes: 15 additions & 4 deletions clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
use crate::rustc::hir::def_id::DefId;
use crate::rustc::hir::*;
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc::ty;
use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc_data_structures::fx::FxHashSet;
use crate::rustc_errors::Applicability;
use crate::syntax::ast::{Lit, LitKind, Name};
use crate::syntax::source_map::{Span, Spanned};
use crate::utils::{get_item_name, in_macro, snippet, span_lint, span_lint_and_sugg, walk_ptrs_ty};
use crate::utils::{get_item_name, in_macro, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty};

/// **What it does:** Checks for getting the length of something via `.len()`
/// just to compare to zero, and suggests using `.is_empty()` where applicable.
Expand Down Expand Up @@ -223,7 +224,15 @@ fn check_cmp(cx: &LateContext<'_, '_>, span: Span, method: &Expr, lit: &Expr, op
}
}

fn check_len(cx: &LateContext<'_, '_>, span: Span, method_name: Name, args: &[Expr], lit: &Lit, op: &str, compare_to: u32) {
fn check_len(
cx: &LateContext<'_, '_>,
span: Span,
method_name: Name,
args: &[Expr],
lit: &Lit,
op: &str,
compare_to: u32,
) {
if let Spanned {
node: LitKind::Int(lit, _),
..
Expand All @@ -235,13 +244,15 @@ fn check_len(cx: &LateContext<'_, '_>, span: Span, method_name: Name, args: &[Ex
}

if method_name == "len" && args.len() == 1 && has_is_empty(cx, &args[0]) {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
LEN_ZERO,
span,
&format!("length comparison to {}", if compare_to == 0 { "zero" } else { "one" }),
"using `is_empty` is clearer and more explicit",
format!("{}{}.is_empty()", op, snippet(cx, args[0].span, "_")),
format!("{}{}.is_empty()", op, snippet_with_applicability(cx, args[0].span, "_", &mut applicability)),
applicability,
);
}
}
Expand Down
6 changes: 6 additions & 0 deletions clippy_lints/src/literal_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use crate::rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc_errors::Applicability;
use crate::syntax::ast::*;
use crate::syntax_pos;
use crate::utils::{snippet_opt, span_lint_and_sugg};
Expand Down Expand Up @@ -300,6 +301,7 @@ impl WarningType {
"mistyped literal suffix",
"did you mean to write",
grouping_hint.to_string(),
Applicability::MaybeIncorrect,
),
WarningType::UnreadableLiteral => span_lint_and_sugg(
cx,
Expand All @@ -308,6 +310,7 @@ impl WarningType {
"long literal lacking separators",
"consider",
grouping_hint.to_owned(),
Applicability::MachineApplicable,
),
WarningType::LargeDigitGroups => span_lint_and_sugg(
cx,
Expand All @@ -316,6 +319,7 @@ impl WarningType {
"digit groups should be smaller",
"consider",
grouping_hint.to_owned(),
Applicability::MachineApplicable,
),
WarningType::InconsistentDigitGrouping => span_lint_and_sugg(
cx,
Expand All @@ -324,6 +328,7 @@ impl WarningType {
"digits grouped inconsistently by underscores",
"consider",
grouping_hint.to_owned(),
Applicability::MachineApplicable,
),
WarningType::DecimalRepresentation => span_lint_and_sugg(
cx,
Expand All @@ -332,6 +337,7 @@ impl WarningType {
"integer literal has a better hexadecimal representation",
"consider",
grouping_hint.to_owned(),
Applicability::MachineApplicable,
),
};
}
Expand Down
Loading

0 comments on commit b2601be

Please sign in to comment.