From 99bba2e933d45e0ff0497aad9c0973360fd786f0 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 11 Aug 2017 14:11:46 +0200 Subject: [PATCH 1/3] Rustup --- clippy_lints/src/cyclomatic_complexity.rs | 21 ++++++++++--- clippy_lints/src/matches.rs | 4 +-- clippy_lints/src/missing_doc.rs | 1 + clippy_lints/src/non_expressive_names.rs | 4 ++- clippy_lints/src/strings.rs | 6 ++-- clippy_lints/src/unicode.rs | 12 +++---- clippy_lints/src/utils/mod.rs | 38 +++++++++++------------ tests/ui/builtin-type-shadow.stderr | 2 ++ tests/ui/for_loop.rs | 2 +- tests/ui/for_loop.stderr | 10 +----- 10 files changed, 52 insertions(+), 48 deletions(-) diff --git a/clippy_lints/src/cyclomatic_complexity.rs b/clippy_lints/src/cyclomatic_complexity.rs index e87e60f9d23a..f2ee62995a01 100644 --- a/clippy_lints/src/cyclomatic_complexity.rs +++ b/clippy_lints/src/cyclomatic_complexity.rs @@ -8,7 +8,7 @@ use rustc::hir::intravisit::{Visitor, walk_expr, NestedVisitorMap}; use syntax::ast::{Attribute, NodeId}; use syntax::codemap::Span; -use utils::{in_macro, LimitStack, span_help_and_lint, paths, match_type}; +use utils::{in_macro, LimitStack, span_help_and_lint, paths, match_type, is_allowed}; /// **What it does:** Checks for methods with high cyclomatic complexity. /// @@ -79,7 +79,16 @@ impl CyclomaticComplexity { }; if cc + divergence < match_arms + short_circuits { - report_cc_bug(cx, cc, match_arms, divergence, short_circuits, ret_adjust, span); + report_cc_bug( + cx, + cc, + match_arms, + divergence, + short_circuits, + ret_adjust, + span, + body.id().node_id, + ); } else { let mut rust_cc = cc + divergence - match_arms - short_circuits; // prevent degenerate cases where unreachable code contains `return` statements @@ -180,7 +189,8 @@ impl<'a, 'tcx> Visitor<'tcx> for CCHelper<'a, 'tcx> { } #[cfg(feature = "debugging")] -fn report_cc_bug(_: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, returns: u64, span: Span) { +#[allow(too_many_arguments)] +fn report_cc_bug(_: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, returns: u64, span: Span, _: NodeId) { span_bug!( span, "Clippy encountered a bug calculating cyclomatic complexity: cc = {}, arms = {}, \ @@ -193,8 +203,9 @@ fn report_cc_bug(_: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, re ); } #[cfg(not(feature = "debugging"))] -fn report_cc_bug(cx: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, returns: u64, span: Span) { - if cx.current_level(CYCLOMATIC_COMPLEXITY) != Level::Allow { +#[allow(too_many_arguments)] +fn report_cc_bug(cx: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, returns: u64, span: Span, id: NodeId) { + if !is_allowed(cx, CYCLOMATIC_COMPLEXITY, id) { cx.sess().span_note_without_error( span, &format!( diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 5dba102f6a41..77050a0e2994 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -12,7 +12,7 @@ use syntax::ast::NodeId; use syntax::codemap::Span; use utils::paths; use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, span_lint_and_sugg, in_external_macro, - expr_block, walk_ptrs_ty, is_expn_of, remove_blocks}; + expr_block, walk_ptrs_ty, is_expn_of, remove_blocks, is_allowed}; use utils::sugg::Sugg; /// **What it does:** Checks for matches with a single arm where an `if let` @@ -194,7 +194,7 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) { return; }; let ty = cx.tables.expr_ty(ex); - if ty.sty != ty::TyBool || cx.current_level(MATCH_BOOL) == Allow { + if ty.sty != ty::TyBool || is_allowed(cx, MATCH_BOOL, ex.id) { check_single_match_single_pattern(cx, ex, arms, expr, els); check_single_match_opt_like(cx, ex, arms, expr, ty, els); } diff --git a/clippy_lints/src/missing_doc.rs b/clippy_lints/src/missing_doc.rs index 8f62494109fa..f0c417f46460 100644 --- a/clippy_lints/src/missing_doc.rs +++ b/clippy_lints/src/missing_doc.rs @@ -22,6 +22,7 @@ // // // +// // rs#L246 // diff --git a/clippy_lints/src/non_expressive_names.rs b/clippy_lints/src/non_expressive_names.rs index a28dc42a8f07..c584e3b1a9e2 100644 --- a/clippy_lints/src/non_expressive_names.rs +++ b/clippy_lints/src/non_expressive_names.rs @@ -104,7 +104,9 @@ fn get_whitelist(interned_name: &str) -> Option<&'static [&'static str]> { } fn whitelisted(interned_name: &str, list: &[&str]) -> bool { - list.iter().any(|&name| interned_name.starts_with(name) || interned_name.ends_with(name)) + list.iter().any(|&name| { + interned_name.starts_with(name) || interned_name.ends_with(name) + }) } impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> { diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs index 9b61f902c492..587ad38c9e50 100644 --- a/clippy_lints/src/strings.rs +++ b/clippy_lints/src/strings.rs @@ -2,7 +2,7 @@ use rustc::hir::*; use rustc::lint::*; use syntax::codemap::Spanned; use utils::SpanlessEq; -use utils::{match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty, get_parent_expr}; +use utils::{match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty, get_parent_expr, is_allowed}; /// **What it does:** Checks for string appends of the form `x = x + y` (without /// `let`!). @@ -83,9 +83,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { if let ExprBinary(Spanned { node: BiAdd, .. }, ref left, _) = e.node { if is_string(cx, left) { - if let Allow = cx.current_level(STRING_ADD_ASSIGN) { - // the string_add_assign is allow, so no duplicates - } else { + if !is_allowed(cx, STRING_ADD_ASSIGN, e.id) { let parent = get_parent_expr(cx, e); if let Some(p) = parent { if let ExprAssign(ref target, _) = p.node { diff --git a/clippy_lints/src/unicode.rs b/clippy_lints/src/unicode.rs index ace8ea7558d1..14d6323de47b 100644 --- a/clippy_lints/src/unicode.rs +++ b/clippy_lints/src/unicode.rs @@ -1,9 +1,9 @@ use rustc::lint::*; use rustc::hir::*; -use syntax::ast::LitKind; +use syntax::ast::{LitKind, NodeId}; use syntax::codemap::Span; use unicode_normalization::UnicodeNormalization; -use utils::{snippet, span_help_and_lint}; +use utils::{snippet, span_help_and_lint, is_allowed}; /// **What it does:** Checks for the Unicode zero-width space in the code. /// @@ -73,7 +73,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unicode { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { if let ExprLit(ref lit) = expr.node { if let LitKind::Str(_, _) = lit.node { - check_str(cx, lit.span) + check_str(cx, lit.span, expr.id) } } } @@ -93,7 +93,7 @@ fn escape>(s: T) -> String { result } -fn check_str(cx: &LateContext, span: Span) { +fn check_str(cx: &LateContext, span: Span, id: NodeId) { let string = snippet(cx, span, ""); if string.contains('\u{200B}') { span_help_and_lint( @@ -115,7 +115,7 @@ fn check_str(cx: &LateContext, span: Span) { "literal non-ASCII character detected", &format!( "Consider replacing the string with:\n\"{}\"", - if cx.current_level(UNICODE_NOT_NFC) == Level::Allow { + if is_allowed(cx, UNICODE_NOT_NFC, id) { escape(string.chars()) } else { escape(string.nfc()) @@ -123,7 +123,7 @@ fn check_str(cx: &LateContext, span: Span) { ), ); } - if cx.current_level(NON_ASCII_LITERAL) == Level::Allow && string.chars().zip(string.nfc()).any(|(a, b)| a != b) { + if is_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) { span_help_and_lint( cx, UNICODE_NOT_NFC, diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 43c0eb68d699..bb79fcef9091 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -4,7 +4,7 @@ use rustc::hir::*; use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX}; use rustc::hir::def::Def; use rustc::hir::map::Node; -use rustc::lint::{LintContext, LateContext, Level, Lint}; +use rustc::lint::{LintContext, Level, LateContext, Lint}; use rustc::session::Session; use rustc::traits; use rustc::ty::{self, TyCtxt, Ty}; @@ -545,10 +545,7 @@ impl<'a> DiagnosticWrapper<'a> { } pub fn span_lint<'a, T: LintContext<'a>>(cx: &T, lint: &'static Lint, sp: Span, msg: &str) { - let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg)); - if cx.current_level(lint) != Level::Allow { - db.wiki_link(lint); - } + DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg)).wiki_link(lint); } pub fn span_help_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>( @@ -559,10 +556,8 @@ pub fn span_help_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>( help: &str, ) { let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, span, msg)); - if cx.current_level(lint) != Level::Allow { - db.0.help(help); - db.wiki_link(lint); - } + db.0.help(help); + db.wiki_link(lint); } pub fn span_note_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>( @@ -574,14 +569,12 @@ pub fn span_note_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>( note: &str, ) { let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, span, msg)); - if cx.current_level(lint) != Level::Allow { - if note_span == span { - db.0.note(note); - } else { - db.0.span_note(note_span, note); - } - db.wiki_link(lint); + if note_span == span { + db.0.note(note); + } else { + db.0.span_note(note_span, note); } + db.wiki_link(lint); } pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>( @@ -594,10 +587,8 @@ pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>( F: for<'b> FnOnce(&mut DiagnosticBuilder<'b>), { let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg)); - if cx.current_level(lint) != Level::Allow { - f(&mut db.0); - db.wiki_link(lint); - } + f(&mut db.0); + db.wiki_link(lint); } pub fn span_lint_and_sugg<'a, 'tcx: 'a, T: LintContext<'tcx>>( @@ -1012,3 +1003,10 @@ pub fn type_size<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> Option bool { + cx.tcx.lint_level_at_node(lint, id).0 == Level::Allow +} diff --git a/tests/ui/builtin-type-shadow.stderr b/tests/ui/builtin-type-shadow.stderr index 29eec0e1d6cc..eb4c73b65c69 100644 --- a/tests/ui/builtin-type-shadow.stderr +++ b/tests/ui/builtin-type-shadow.stderr @@ -9,6 +9,8 @@ error: This generic shadows the built-in type `u32` error[E0308]: mismatched types --> $DIR/builtin-type-shadow.rs:6:5 | +5 | fn foo(a: u32) -> u32 { + | --- expected `u32` because of return type 6 | 42 | ^^ expected type parameter, found integral variable | diff --git a/tests/ui/for_loop.rs b/tests/ui/for_loop.rs index 1a4b97650630..0dbebcdca2cf 100644 --- a/tests/ui/for_loop.rs +++ b/tests/ui/for_loop.rs @@ -75,7 +75,7 @@ impl Unrelated { #[warn(needless_range_loop, explicit_iter_loop, explicit_into_iter_loop, iter_next_loop, reverse_range_loop, explicit_counter_loop, for_kv_map)] #[warn(unused_collect)] #[allow(linkedlist, shadow_unrelated, unnecessary_mut_passed, cyclomatic_complexity, similar_names)] -#[allow(many_single_char_names)] +#[allow(many_single_char_names, unused_variables)] fn main() { const MAX_LEN: usize = 42; diff --git a/tests/ui/for_loop.stderr b/tests/ui/for_loop.stderr index f350547f6c3b..d4954357665e 100644 --- a/tests/ui/for_loop.stderr +++ b/tests/ui/for_loop.stderr @@ -84,14 +84,6 @@ help: consider using an iterator 84 | for in &vec { | ^^^^^^ -error: unused variable: `i` - --> $DIR/for_loop.rs:88:9 - | -88 | for i in 0..vec.len() { - | ^ - | - = note: `-D unused-variables` implied by `-D warnings` - error: the loop variable `i` is only used to index `vec`. --> $DIR/for_loop.rs:93:5 | @@ -506,5 +498,5 @@ help: use the corresponding method 344 | for k in rm.keys() { | ^ -error: aborting due to 51 previous errors +error: aborting due to 50 previous errors From 2fb59c9723d69184450952767172def2ef03e6ce Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Sun, 13 Aug 2017 20:57:55 +0200 Subject: [PATCH 2/3] Bump the version --- CHANGELOG.md | 3 +++ Cargo.toml | 4 ++-- clippy_lints/Cargo.toml | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c72ac8686fe..b55b55943a7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Change Log All notable changes to this project will be documented in this file. +## 0.0.151 +* Update to *rustc 1.21.0-nightly (13d94d5fa 2017-08-10)* + ## 0.0.150 * Update to *rustc 1.21.0-nightly (215e0b10e 2017-08-08)* diff --git a/Cargo.toml b/Cargo.toml index a5c615e232c8..7ab436214348 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy" -version = "0.0.150" +version = "0.0.151" authors = [ "Manish Goregaokar ", "Andre Bogus ", @@ -31,7 +31,7 @@ path = "src/main.rs" [dependencies] # begin automatic update -clippy_lints = { version = "0.0.150", path = "clippy_lints" } +clippy_lints = { version = "0.0.151", path = "clippy_lints" } # end automatic update cargo_metadata = "0.2" diff --git a/clippy_lints/Cargo.toml b/clippy_lints/Cargo.toml index 5072175d99cb..2be46842aac9 100644 --- a/clippy_lints/Cargo.toml +++ b/clippy_lints/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "clippy_lints" # begin automatic update -version = "0.0.150" +version = "0.0.151" # end automatic update authors = [ "Manish Goregaokar ", From ab167d27f4299700b6b8522e5b0034f70063285c Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 14 Aug 2017 09:51:16 +0200 Subject: [PATCH 3/3] Update for rustc output changes --- tests/ui/shadow.stderr | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/ui/shadow.stderr b/tests/ui/shadow.stderr index 9501fd68fcec..3fc2b7234f78 100644 --- a/tests/ui/shadow.stderr +++ b/tests/ui/shadow.stderr @@ -1,8 +1,8 @@ error: `x` is shadowed by itself in `&mut x` - --> $DIR/shadow.rs:13:9 + --> $DIR/shadow.rs:13:5 | 13 | let x = &mut x; - | ^^^^^^^^^^ + | ^^^^^^^^^^^^^^ | = note: `-D shadow-same` implied by `-D warnings` note: previous binding is here @@ -12,10 +12,10 @@ note: previous binding is here | ^ error: `x` is shadowed by itself in `{ x }` - --> $DIR/shadow.rs:14:9 + --> $DIR/shadow.rs:14:5 | 14 | let x = { x }; - | ^^^^^^^^^ + | ^^^^^^^^^^^^^ | note: previous binding is here --> $DIR/shadow.rs:13:9 @@ -24,10 +24,10 @@ note: previous binding is here | ^ error: `x` is shadowed by itself in `(&*x)` - --> $DIR/shadow.rs:15:9 + --> $DIR/shadow.rs:15:5 | 15 | let x = (&*x); - | ^^^^^^^^^ + | ^^^^^^^^^^^^^ | note: previous binding is here --> $DIR/shadow.rs:14:9 @@ -123,10 +123,10 @@ note: previous binding is here | ^ error: `x` shadows a previous declaration - --> $DIR/shadow.rs:23:9 + --> $DIR/shadow.rs:23:5 | 23 | let x; - | ^ + | ^^^^^ | note: previous binding is here --> $DIR/shadow.rs:21:9