Skip to content

Commit 617079a

Browse files
committed
Rustup
1 parent 5622f9a commit 617079a

File tree

8 files changed

+39
-47
lines changed

8 files changed

+39
-47
lines changed

clippy_lints/src/cyclomatic_complexity.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc::hir::intravisit::{Visitor, walk_expr, NestedVisitorMap};
88
use syntax::ast::{Attribute, NodeId};
99
use syntax::codemap::Span;
1010

11-
use utils::{in_macro, LimitStack, span_help_and_lint, paths, match_type};
11+
use utils::{in_macro, LimitStack, span_help_and_lint, paths, match_type, is_allowed};
1212

1313
/// **What it does:** Checks for methods with high cyclomatic complexity.
1414
///
@@ -72,7 +72,7 @@ impl CyclomaticComplexity {
7272
};
7373

7474
if cc + divergence < match_arms + short_circuits {
75-
report_cc_bug(cx, cc, match_arms, divergence, short_circuits, ret_adjust, span);
75+
report_cc_bug(cx, cc, match_arms, divergence, short_circuits, ret_adjust, span, body.id().node_id);
7676
} else {
7777
let mut rust_cc = cc + divergence - match_arms - short_circuits;
7878
// prevent degenerate cases where unreachable code contains `return` statements
@@ -163,7 +163,8 @@ impl<'a, 'tcx> Visitor<'tcx> for CCHelper<'a, 'tcx> {
163163
}
164164

165165
#[cfg(feature="debugging")]
166-
fn report_cc_bug(_: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, returns: u64, span: Span) {
166+
#[allow(too_many_arguments)]
167+
fn report_cc_bug(_: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, returns: u64, span: Span, _: NodeId) {
167168
span_bug!(span,
168169
"Clippy encountered a bug calculating cyclomatic complexity: cc = {}, arms = {}, \
169170
div = {}, shorts = {}, returns = {}. Please file a bug report.",
@@ -174,8 +175,9 @@ fn report_cc_bug(_: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, re
174175
returns);
175176
}
176177
#[cfg(not(feature="debugging"))]
177-
fn report_cc_bug(cx: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, returns: u64, span: Span) {
178-
if cx.current_level(CYCLOMATIC_COMPLEXITY) != Level::Allow {
178+
#[allow(too_many_arguments)]
179+
fn report_cc_bug(cx: &LateContext, cc: u64, narms: u64, div: u64, shorts: u64, returns: u64, span: Span, id: NodeId) {
180+
if !is_allowed(cx, CYCLOMATIC_COMPLEXITY, id) {
179181
cx.sess().span_note_without_error(span,
180182
&format!("Clippy encountered a bug calculating cyclomatic complexity \
181183
(hide this message with `#[allow(cyclomatic_complexity)]`): \

clippy_lints/src/matches.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use syntax::ast::NodeId;
1212
use syntax::codemap::Span;
1313
use utils::paths;
1414
use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, span_lint_and_sugg, in_external_macro,
15-
expr_block, walk_ptrs_ty, is_expn_of, remove_blocks};
15+
expr_block, walk_ptrs_ty, is_expn_of, remove_blocks, is_allowed};
1616
use utils::sugg::Sugg;
1717

1818
/// **What it does:** Checks for matches with a single arm where an `if let`
@@ -192,7 +192,7 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
192192
return;
193193
};
194194
let ty = cx.tables.expr_ty(ex);
195-
if ty.sty != ty::TyBool || cx.current_level(MATCH_BOOL) == Allow {
195+
if ty.sty != ty::TyBool || is_allowed(cx, MATCH_BOOL, ex.id) {
196196
check_single_match_single_pattern(cx, ex, arms, expr, els);
197197
check_single_match_opt_like(cx, ex, arms, expr, ty, els);
198198
}

clippy_lints/src/strings.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc::hir::*;
22
use rustc::lint::*;
33
use syntax::codemap::Spanned;
44
use utils::SpanlessEq;
5-
use utils::{match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty, get_parent_expr};
5+
use utils::{match_type, paths, span_lint, span_lint_and_sugg, walk_ptrs_ty, get_parent_expr, is_allowed};
66

77
/// **What it does:** Checks for string appends of the form `x = x + y` (without
88
/// `let`!).
@@ -83,9 +83,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
8383
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
8484
if let ExprBinary(Spanned { node: BiAdd, .. }, ref left, _) = e.node {
8585
if is_string(cx, left) {
86-
if let Allow = cx.current_level(STRING_ADD_ASSIGN) {
87-
// the string_add_assign is allow, so no duplicates
88-
} else {
86+
if !is_allowed(cx, STRING_ADD_ASSIGN, e.id) {
8987
let parent = get_parent_expr(cx, e);
9088
if let Some(p) = parent {
9189
if let ExprAssign(ref target, _) = p.node {

clippy_lints/src/unicode.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use rustc::lint::*;
22
use rustc::hir::*;
3-
use syntax::ast::LitKind;
3+
use syntax::ast::{LitKind, NodeId};
44
use syntax::codemap::Span;
55
use unicode_normalization::UnicodeNormalization;
6-
use utils::{snippet, span_help_and_lint};
6+
use utils::{snippet, span_help_and_lint, is_allowed};
77

88
/// **What it does:** Checks for the Unicode zero-width space in the code.
99
///
@@ -72,7 +72,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unicode {
7272
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
7373
if let ExprLit(ref lit) = expr.node {
7474
if let LitKind::Str(_, _) = lit.node {
75-
check_str(cx, lit.span)
75+
check_str(cx, lit.span, expr.id)
7676
}
7777
}
7878
}
@@ -92,7 +92,7 @@ fn escape<T: Iterator<Item = char>>(s: T) -> String {
9292
result
9393
}
9494

95-
fn check_str(cx: &LateContext, span: Span) {
95+
fn check_str(cx: &LateContext, span: Span, id: NodeId) {
9696
let string = snippet(cx, span, "");
9797
if string.contains('\u{200B}') {
9898
span_help_and_lint(cx,
@@ -108,13 +108,13 @@ fn check_str(cx: &LateContext, span: Span) {
108108
span,
109109
"literal non-ASCII character detected",
110110
&format!("Consider replacing the string with:\n\"{}\"",
111-
if cx.current_level(UNICODE_NOT_NFC) == Level::Allow {
111+
if is_allowed(cx, UNICODE_NOT_NFC, id) {
112112
escape(string.chars())
113113
} else {
114114
escape(string.nfc())
115115
}));
116116
}
117-
if cx.current_level(NON_ASCII_LITERAL) == Level::Allow && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
117+
if is_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
118118
span_help_and_lint(cx,
119119
UNICODE_NOT_NFC,
120120
span,

clippy_lints/src/utils/mod.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::hir::*;
44
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
55
use rustc::hir::def::Def;
66
use rustc::hir::map::Node;
7-
use rustc::lint::{LintContext, LateContext, Level, Lint};
7+
use rustc::lint::{LintContext, Level, LateContext, Lint};
88
use rustc::session::Session;
99
use rustc::traits;
1010
use rustc::ty::{self, TyCtxt, Ty};
@@ -505,10 +505,7 @@ impl<'a> DiagnosticWrapper<'a> {
505505
}
506506

507507
pub fn span_lint<'a, T: LintContext<'a>>(cx: &T, lint: &'static Lint, sp: Span, msg: &str) {
508-
let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg));
509-
if cx.current_level(lint) != Level::Allow {
510-
db.wiki_link(lint);
511-
}
508+
DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg)).wiki_link(lint);
512509
}
513510

514511
pub fn span_help_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>(
@@ -519,10 +516,8 @@ pub fn span_help_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>(
519516
help: &str
520517
) {
521518
let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, span, msg));
522-
if cx.current_level(lint) != Level::Allow {
523-
db.0.help(help);
524-
db.wiki_link(lint);
525-
}
519+
db.0.help(help);
520+
db.wiki_link(lint);
526521
}
527522

528523
pub fn span_note_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>(
@@ -534,14 +529,12 @@ pub fn span_note_and_lint<'a, 'tcx: 'a, T: LintContext<'tcx>>(
534529
note: &str
535530
) {
536531
let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, span, msg));
537-
if cx.current_level(lint) != Level::Allow {
538-
if note_span == span {
539-
db.0.note(note);
540-
} else {
541-
db.0.span_note(note_span, note);
542-
}
543-
db.wiki_link(lint);
532+
if note_span == span {
533+
db.0.note(note);
534+
} else {
535+
db.0.span_note(note_span, note);
544536
}
537+
db.wiki_link(lint);
545538
}
546539

547540
pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>(
@@ -553,10 +546,8 @@ pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>(
553546
) where F: for<'b> FnOnce(&mut DiagnosticBuilder<'b>)
554547
{
555548
let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg));
556-
if cx.current_level(lint) != Level::Allow {
557-
f(&mut db.0);
558-
db.wiki_link(lint);
559-
}
549+
f(&mut db.0);
550+
db.wiki_link(lint);
560551
}
561552

562553
pub fn span_lint_and_sugg<'a, 'tcx: 'a, T: LintContext<'tcx>>(
@@ -950,3 +941,10 @@ pub fn is_try(expr: &Expr) -> Option<&Expr> {
950941
pub fn type_size<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> Option<u64> {
951942
ty.layout(cx.tcx, cx.param_env).ok().map(|layout| layout.size(cx.tcx).bytes())
952943
}
944+
945+
/// Returns true if the lint is allowed in the current context
946+
///
947+
/// Useful for skipping long running code when it's unnecessary
948+
pub fn is_allowed(cx: &LateContext, lint: &'static Lint, id: NodeId) -> bool {
949+
cx.tcx.lint_level_at_node(lint, id).0 == Level::Allow
950+
}

tests/ui/builtin-type-shadow.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ error: This generic shadows the built-in type `u32`
99
error[E0308]: mismatched types
1010
--> $DIR/builtin-type-shadow.rs:6:5
1111
|
12+
5 | fn foo<u32>(a: u32) -> u32 {
13+
| --- expected `u32` because of return type
1214
6 | 42
1315
| ^^ expected type parameter, found integral variable
1416
|

tests/ui/for_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Unrelated {
7575
#[warn(needless_range_loop, explicit_iter_loop, explicit_into_iter_loop, iter_next_loop, reverse_range_loop, explicit_counter_loop, for_kv_map)]
7676
#[warn(unused_collect)]
7777
#[allow(linkedlist, shadow_unrelated, unnecessary_mut_passed, cyclomatic_complexity, similar_names)]
78-
#[allow(many_single_char_names)]
78+
#[allow(many_single_char_names, unused_variables)]
7979
fn main() {
8080
const MAX_LEN: usize = 42;
8181

tests/ui/for_loop.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,6 @@ help: consider using an iterator
8484
84 | for <item> in &vec {
8585
| ^^^^^^
8686

87-
error: unused variable: `i`
88-
--> $DIR/for_loop.rs:88:9
89-
|
90-
88 | for i in 0..vec.len() {
91-
| ^
92-
|
93-
= note: `-D unused-variables` implied by `-D warnings`
94-
9587
error: the loop variable `i` is only used to index `vec`.
9688
--> $DIR/for_loop.rs:93:5
9789
|
@@ -506,5 +498,5 @@ help: use the corresponding method
506498
344 | for k in rm.keys() {
507499
| ^
508500

509-
error: aborting due to 51 previous errors
501+
error: aborting due to 50 previous errors
510502

0 commit comments

Comments
 (0)