Skip to content

Commit

Permalink
remove nondeterminism by adjusting thresholds
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Dec 21, 2016
1 parent 299d7be commit ed9d71f
Show file tree
Hide file tree
Showing 30 changed files with 157 additions and 133 deletions.
5 changes: 2 additions & 3 deletions clippy_lints/src/array_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
}

/// Returns an option containing a tuple with the start and end (exclusive) of the range.
fn to_const_range(start: Option<Option<ConstVal>>, end: Option<Option<ConstVal>>, limits: RangeLimits,
array_size: ConstInt)
-> Option<(ConstInt, ConstInt)> {
fn to_const_range(start: Option<Option<ConstVal>>, end: Option<Option<ConstVal>>, limits: RangeLimits, array_size: ConstInt)
-> Option<(ConstInt, ConstInt)> {
let start = match start {
Some(Some(ConstVal::Integral(x))) => x,
Some(_) => return None,
Expand Down
14 changes: 7 additions & 7 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ declare_lint! {

/// **What it does:** Checks for `extern crate` and `use` items annotated with lint attributes
///
/// **Why is this bad?** Lint attributes have no effect on crate imports. Most likely a `!` was forgotten
/// **Why is this bad?** Lint attributes have no effect on crate imports. Most likely a `!` was
/// forgotten
///
/// **Known problems:** Technically one might allow `unused_import` on a `use` item, but it's easier to remove the unused item.
/// **Known problems:** Technically one might allow `unused_import` on a `use` item,
/// but it's easier to remove the unused item.
///
/// **Example:**
/// ```rust
Expand Down Expand Up @@ -125,11 +127,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
attr.span,
"useless lint attribute",
|db| {
sugg.insert(1, '!');
db.span_suggestion(attr.span,
"if you just forgot a `!`, use",
sugg);
});
sugg.insert(1, '!');
db.span_suggestion(attr.span, "if you just forgot a `!`, use", sugg);
});
}
}
},
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 @@ -204,7 +204,8 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) {

if let PatKind::Wild = j.pats[0].node {
// if the last arm is _, then i could be integrated into _
// note that i.pats[0] cannot be _, because that would mean that we're hiding all the subsequent arms, and rust won't compile
// note that i.pats[0] cannot be _, because that would mean that we're
// hiding all the subsequent arms, and rust won't compile
db.span_note(i.body.span,
&format!("`{}` has the same arm body as the `_` wildcard, consider removing it`",
lhs));
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item, trait_ref
item.span,
"you are implementing `Clone` explicitly on a `Copy` type",
|db| {
db.span_note(item.span, "consider deriving `Clone` or removing `Copy`");
});
db.span_note(item.span, "consider deriving `Clone` or removing `Copy`");
});
}
}
4 changes: 3 additions & 1 deletion clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ fn check_doc(cx: &EarlyContext, valid_idents: &[String], docs: &[(String, Span)]
}

lookup_parser = parser.clone();
if let (Some((false, $c)), Some((false, $c))) = (lookup_parser.next(), lookup_parser.next()) {
let a = lookup_parser.next();
let b = lookup_parser.next();
if let (Some((false, $c)), Some((false, $c))) = (a, b) {
let mut close_count = 3;
while let Some((false, $c)) = lookup_parser.next() {
close_count += 1;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapLint {
}

fn check_cond<'a, 'tcx, 'b>(cx: &'a LateContext<'a, 'tcx>, check: &'b Expr)
-> Option<(&'static str, &'b Expr, &'b Expr)> {
-> Option<(&'static str, &'b Expr, &'b Expr)> {
if_let_chain! {[
let ExprMethodCall(ref name, _, ref params) = check.node,
params.len() >= 2,
Expand Down
12 changes: 7 additions & 5 deletions clippy_lints/src/enum_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ declare_lint! {

/// **What it does:** Checks for modules that have the same name as their parent module
///
/// **Why is this bad?** A typical beginner mistake is to have `mod foo;` and again `mod foo { .. }` in `foo.rs`.
/// The expectation is that items inside the inner `mod foo { .. }` are then available
/// **Why is this bad?** A typical beginner mistake is to have `mod foo;` and again `mod foo { ..
/// }` in `foo.rs`.
/// The expectation is that items inside the inner `mod foo { .. }` are then
/// available
/// through `foo::x`, but they are only available through `foo::foo::x`.
/// If this is done on purpose, it would be better to choose a more representative module name.
/// If this is done on purpose, it would be better to choose a more
/// representative module name.
///
/// **Known problems:** None.
///
Expand Down Expand Up @@ -111,8 +114,7 @@ fn partial_rmatch(post: &str, name: &str) -> usize {

// FIXME: #600
#[allow(while_let_on_iterator)]
fn check_variant(cx: &EarlyContext, threshold: u64, def: &EnumDef, item_name: &str, item_name_chars: usize,
span: Span) {
fn check_variant(cx: &EarlyContext, threshold: u64, def: &EnumDef, item_name: &str, item_name_chars: usize, span: Span) {
if (def.variants.len() as u64) < threshold {
return;
}
Expand Down
6 changes: 4 additions & 2 deletions clippy_lints/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ impl LintPass for Pass {
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
fn check_fn(&mut self, cx: &LateContext<'a, 'tcx>, _: visit::FnKind<'tcx>, decl: &'tcx FnDecl, body: &'tcx Expr,
_: Span, id: NodeId) {
fn check_fn(
&mut self, cx: &LateContext<'a, 'tcx>, _: visit::FnKind<'tcx>, decl: &'tcx FnDecl, body: &'tcx Expr, _: Span,
id: NodeId
) {
let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);

let infcx = cx.tcx.borrowck_fake_infer_ctxt(param_env);
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/eval_order_dependence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> {
}
},
_ => {
// do not lint expressions referencing objects of type `!`, as that required a diverging expression to begin with
// do not lint expressions referencing objects of type `!`, as that required a diverging expression
// to begin with
},
}
self.maybe_walk_expr(e);
Expand Down
12 changes: 8 additions & 4 deletions clippy_lints/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ impl LintPass for Functions {
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
fn check_fn(&mut self, cx: &LateContext<'a, 'tcx>, kind: intravisit::FnKind<'tcx>, decl: &'tcx hir::FnDecl,
expr: &'tcx hir::Expr, span: Span, nodeid: ast::NodeId) {
fn check_fn(
&mut self, cx: &LateContext<'a, 'tcx>, kind: intravisit::FnKind<'tcx>, decl: &'tcx hir::FnDecl,
expr: &'tcx hir::Expr, span: Span, nodeid: ast::NodeId
) {
use rustc::hir::map::Node::*;

let is_impl = if let Some(NodeItem(item)) = cx.tcx.map.find(cx.tcx.map.get_parent_node(nodeid)) {
Expand Down Expand Up @@ -124,8 +126,10 @@ impl<'a, 'tcx> Functions {
}
}

fn check_raw_ptr(&self, cx: &LateContext<'a, 'tcx>, unsafety: hir::Unsafety, decl: &'tcx hir::FnDecl,
expr: &'tcx hir::Expr, nodeid: ast::NodeId) {
fn check_raw_ptr(
&self, cx: &LateContext<'a, 'tcx>, unsafety: hir::Unsafety, decl: &'tcx hir::FnDecl, expr: &'tcx hir::Expr,
nodeid: ast::NodeId
) {
if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(nodeid) {
let raw_ptrs = decl.inputs.iter().filter_map(|arg| raw_ptr_arg(cx, arg)).collect::<HashSet<_>>();

Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/let_if_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
}

fn check_assign<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: hir::def_id::DefId, block: &'tcx hir::Block)
-> Option<&'tcx hir::Expr> {
-> Option<&'tcx hir::Expr> {
if_let_chain! {[
block.expr.is_none(),
let Some(expr) = block.stmts.iter().last(),
Expand Down
6 changes: 2 additions & 4 deletions clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,8 @@ fn check_fn_inner<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: &'tcx FnDecl, gene
report_extra_lifetimes(cx, decl, generics);
}

fn could_use_elision<'a, 'tcx: 'a, T: Iterator<Item = &'tcx Lifetime>>(cx: &LateContext<'a, 'tcx>,
func: &'tcx FnDecl,
named_lts: &'tcx [LifetimeDef], bounds_lts: T)
-> bool {
fn could_use_elision<'a, 'tcx: 'a, T: Iterator<Item = &'tcx Lifetime>>(cx: &LateContext<'a, 'tcx>, func: &'tcx FnDecl, named_lts: &'tcx [LifetimeDef], bounds_lts: T)
-> bool {
// There are two scenarios where elision works:
// * no output references, all input references have different LT
// * output references, exactly one input reference with same LT
Expand Down
28 changes: 13 additions & 15 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
expr.span,
"this loop could be written as a `while let` loop",
|db| {
let sug = format!("while let {} = {} {{ .. }}",
snippet(cx, arms[0].pats[0].span, ".."),
snippet(cx, matchexpr.span, ".."));
db.span_suggestion(expr.span, "try", sug);
});
let sug = format!("while let {} = {} {{ .. }}",
snippet(cx, arms[0].pats[0].span, ".."),
snippet(cx, matchexpr.span, ".."));
db.span_suggestion(expr.span, "try", sug);
});
}
},
_ => (),
Expand All @@ -379,10 +379,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
expr.span,
"this loop could be written as a `for` loop",
|db| {
db.span_suggestion(expr.span,
"try",
format!("for {} in {} {{ .. }}", loop_var, iterator));
});
db.span_suggestion(expr.span, "try", format!("for {} in {} {{ .. }}", loop_var, iterator));
});
}
}
}
Expand Down Expand Up @@ -473,11 +471,11 @@ fn check_for_loop_range<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat, ar
expr.span,
&format!("the loop variable `{}` is used to index `{}`", ident.node, indexed),
|db| {
multispan_sugg(db,
multispan_sugg(db,
"consider using an iterator".to_string(),
&[(pat.span, &format!("({}, <item>)", ident.node)),
(arg.span, &format!("{}.iter().enumerate(){}{}", indexed, take, skip))]);
});
});
} else {
let repl = if starts_at_zero && take.is_empty() {
format!("&{}", indexed)
Expand All @@ -492,10 +490,10 @@ fn check_for_loop_range<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat, ar
ident.node,
indexed),
|db| {
multispan_sugg(db,
"consider using an iterator".to_string(),
&[(pat.span, "<item>"), (arg.span, &repl)]);
});
multispan_sugg(db,
"consider using an iterator".to_string(),
&[(pat.span, "<item>"), (arg.span, &repl)]);
});
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ fn check_match_ref_pats(cx: &LateContext, ex: &Expr, arms: &[Arm], source: Match
expr.span,
"you don't need to add `&` to both the expression and the patterns",
|db| {
let inner = Sugg::hir(cx, inner, "..");
let template = match_template(expr.span, source, inner);
db.span_suggestion(expr.span, "try", template);
});
let inner = Sugg::hir(cx, inner, "..");
let template = match_template(expr.span, source, inner);
db.span_suggestion(expr.span, "try", template);
});
} else {
span_lint_and_then(cx,
MATCH_REF_PATS,
Expand All @@ -335,8 +335,7 @@ fn check_match_ref_pats(cx: &LateContext, ex: &Expr, arms: &[Arm], source: Match
let ex = Sugg::hir(cx, ex, "..");
let template = match_template(expr.span, source, ex.deref());
db.span_suggestion(expr.span,
"instead of prefixing all patterns with `&`, you can \
dereference the expression",
"instead of prefixing all patterns with `&`, you can dereference the expression",
template);
});
}
Expand Down
34 changes: 18 additions & 16 deletions clippy_lints/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
/// Checks for the `OR_FUN_CALL` lint.
fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir::Expr]) {
/// Check for `unwrap_or(T::new())` or `unwrap_or(T::default())`.
fn check_unwrap_or_default(cx: &LateContext, name: &str, fun: &hir::Expr, self_expr: &hir::Expr, arg: &hir::Expr,
or_has_args: bool, span: Span)
-> bool {
fn check_unwrap_or_default(
cx: &LateContext, name: &str, fun: &hir::Expr, self_expr: &hir::Expr, arg: &hir::Expr, or_has_args: bool,
span: Span
) -> bool {
if or_has_args {
return false;
}
Expand All @@ -721,11 +722,10 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
span,
&format!("use of `{}` followed by a call to `{}`", name, path),
|db| {
db.span_suggestion(span,
"try this",
format!("{}.unwrap_or_default()",
snippet(cx, self_expr.span, "_")));
});
db.span_suggestion(span,
"try this",
format!("{}.unwrap_or_default()", snippet(cx, self_expr.span, "_")));
});
return true;
}
}
Expand All @@ -736,8 +736,10 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
}

/// Check for `*or(foo())`.
fn check_general_case(cx: &LateContext, name: &str, fun: &hir::Expr, self_expr: &hir::Expr, arg: &hir::Expr,
or_has_args: bool, span: Span) {
fn check_general_case(
cx: &LateContext, name: &str, fun: &hir::Expr, self_expr: &hir::Expr, arg: &hir::Expr, or_has_args: bool,
span: Span
) {
// don't lint for constant values
// FIXME: can we `expect` here instead of match?
if let Some(qualif) = cx.tcx.const_qualif_map.borrow().get(&arg.id) {
Expand Down Expand Up @@ -776,10 +778,10 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[hir:
span,
&format!("use of `{}` followed by a function call", name),
|db| {
db.span_suggestion(span,
db.span_suggestion(span,
"try this",
format!("{}.{}_{}({})", snippet(cx, self_expr.span, "_"), name, suffix, sugg));
});
});
}

if args.len() == 2 {
Expand Down Expand Up @@ -836,10 +838,10 @@ fn lint_vec_extend(cx: &LateContext, expr: &hir::Expr, args: &[hir::Expr]) {
expr.span,
"use of `extend` to extend a Vec by a slice",
|db| {
db.span_suggestion(expr.span,
db.span_suggestion(expr.span,
"try this",
format!("{}.extend_from_slice({})", snippet(cx, args[0].span, "_"), slice));
});
});
}
}

Expand Down Expand Up @@ -1223,8 +1225,8 @@ fn lint_single_char_pattern(cx: &LateContext, expr: &hir::Expr, arg: &hir::Expr)
arg.span,
"single-character string constant used as pattern",
|db| {
db.span_suggestion(expr.span, "try using a char instead:", hint);
});
db.span_suggestion(expr.span, "try using a char instead:", hint);
});
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/misc_early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,11 @@ impl EarlyLintPass for MiscEarly {
expr.span,
"Try not to call a closure in the expression where it is declared.",
|db| {
if decl.inputs.is_empty() {
let hint = snippet(cx, block.span, "..").into_owned();
db.span_suggestion(expr.span, "Try doing something like: ", hint);
}
});
if decl.inputs.is_empty() {
let hint = snippet(cx, block.span, "..").into_owned();
db.span_suggestion(expr.span, "Try doing something like: ", hint);
}
});
}
}
},
Expand Down
Loading

0 comments on commit ed9d71f

Please sign in to comment.