Skip to content

Commit 65b0554

Browse files
committed
note individual lint name set via lint group attribute in notes
Warning or error messages set via a lint group attribute (e.g. `#[deny(warnings)]`) should still make it clear which individual lint (by name) was triggered, similarly to how we include "on by default" language for default lints. This—and, while we're here, the existing "on by default" language—can be tucked into a note rather than cluttering the main error message. This occasions the slightest of refactorings (we now have to get the diagnostic-builder with the main message first, before matching on the lint source). This is in the matter of #36846.
1 parent 8967085 commit 65b0554

File tree

11 files changed

+72
-39
lines changed

11 files changed

+72
-39
lines changed

src/librustc/lint/context.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use std::cmp;
4040
use std::default::Default as StdDefault;
4141
use std::mem;
4242
use std::fmt;
43+
use std::ops::Deref;
4344
use syntax::attr;
4445
use syntax::ast;
4546
use syntax_pos::{MultiSpan, Span};
@@ -446,42 +447,46 @@ pub fn raw_struct_lint<'a, S>(sess: &'a Session,
446447
-> DiagnosticBuilder<'a>
447448
where S: Into<MultiSpan>
448449
{
449-
let (mut level, source) = lvlsrc;
450+
let (level, source) = lvlsrc;
450451
if level == Allow {
451452
return sess.diagnostic().struct_dummy();
452453
}
453454

454455
let name = lint.name_lower();
455456
let mut def = None;
456-
let msg = match source {
457-
Default => {
458-
format!("{}, #[{}({})] on by default", msg,
459-
level.as_str(), name)
460-
},
461-
CommandLine => {
462-
format!("{} [-{} {}]", msg,
463-
match level {
464-
Warn => 'W', Deny => 'D', Forbid => 'F',
465-
Allow => bug!()
466-
}, name.replace("_", "-"))
467-
},
468-
Node(src) => {
469-
def = Some(src);
470-
msg.to_string()
471-
}
472-
};
473457

474-
// For purposes of printing, we can treat forbid as deny.
475-
if level == Forbid { level = Deny; }
458+
// Except for possible note details, forbid behaves like deny.
459+
let effective_level = if level == Forbid { Deny } else { level };
476460

477-
let mut err = match (level, span) {
461+
let mut err = match (effective_level, span) {
478462
(Warn, Some(sp)) => sess.struct_span_warn(sp, &msg[..]),
479463
(Warn, None) => sess.struct_warn(&msg[..]),
480464
(Deny, Some(sp)) => sess.struct_span_err(sp, &msg[..]),
481465
(Deny, None) => sess.struct_err(&msg[..]),
482466
_ => bug!("impossible level in raw_emit_lint"),
483467
};
484468

469+
match source {
470+
Default => {
471+
err.note(&format!("#[{}({})] on by default", level.as_str(), name));
472+
},
473+
CommandLine => {
474+
err.note(&format!("[-{} {}]",
475+
match level {
476+
Warn => 'W', Deny => 'D', Forbid => 'F',
477+
Allow => bug!()
478+
}, name.replace("_", "-")));
479+
},
480+
Node(lint_attr_name, src) => {
481+
def = Some(src);
482+
if lint_attr_name.as_str().deref() != name {
483+
let level_str = level.as_str();
484+
err.note(&format!("#[{}({})] implies #[{}({})]",
485+
level_str, lint_attr_name, level_str, name));
486+
}
487+
}
488+
}
489+
485490
// Check for future incompatibility lints and issue a stronger warning.
486491
if let Some(future_incompatible) = lints.future_incompatible(LintId::of(lint)) {
487492
let explanation = format!("this was previously accepted by the compiler \
@@ -649,6 +654,8 @@ pub trait LintContext<'tcx>: Sized {
649654
}
650655
};
651656

657+
let lint_attr_name = result.expect("lint attribute should be well-formed").0;
658+
652659
for (lint_id, level, span) in v {
653660
let (now, now_source) = self.lints().get_level_source(lint_id);
654661
if now == Forbid && level != Forbid {
@@ -660,7 +667,7 @@ pub trait LintContext<'tcx>: Sized {
660667
diag_builder.span_label(span, &format!("overruled by previous forbid"));
661668
match now_source {
662669
LintSource::Default => &mut diag_builder,
663-
LintSource::Node(forbid_source_span) => {
670+
LintSource::Node(_, forbid_source_span) => {
664671
diag_builder.span_label(forbid_source_span,
665672
&format!("`forbid` level set here"))
666673
},
@@ -672,7 +679,7 @@ pub trait LintContext<'tcx>: Sized {
672679
let src = self.lints().get_level_source(lint_id).1;
673680
self.level_stack().push((lint_id, (now, src)));
674681
pushed += 1;
675-
self.mut_lints().set_level(lint_id, (level, Node(span)));
682+
self.mut_lints().set_level(lint_id, (level, Node(lint_attr_name, span)));
676683
}
677684
}
678685
}

src/librustc/lint/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ pub enum LintSource {
338338
Default,
339339

340340
/// Lint level was set by an attribute.
341-
Node(Span),
341+
Node(ast::Name, Span),
342342

343343
/// Lint level was set by a command-line flag.
344344
CommandLine,

src/test/compile-fail/imports/rfc-1560-warning-cycle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod bar {
2323
//~^ WARN `Foo` is ambiguous
2424
//~| WARN hard error in a future release
2525
//~| NOTE see issue #38260
26+
//~| NOTE #[warn(legacy_imports)] on by default
2627
}
2728
}
2829

src/test/compile-fail/issue-30730.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
// except according to those terms.
1010

1111
#![deny(warnings)] //~ NOTE: lint level defined here
12-
use std::thread; //~ ERROR: unused import
12+
use std::thread;
13+
//~^ ERROR: unused import
14+
//~| NOTE: #[deny(warnings)] implies #[deny(unused_imports)]
1315
fn main() {}

src/test/compile-fail/lint-group-style.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
//~^ NOTE lint level defined here
1313
#![allow(dead_code)]
1414

15-
fn CamelCase() {} //~ ERROR function `CamelCase` should have a snake case name
15+
fn CamelCase() {}
16+
//~^ ERROR function `CamelCase` should have a snake case name
17+
//~| NOTE #[deny(bad_style)] implies #[deny(non_snake_case)]
1618

1719
#[allow(bad_style)]
1820
mod test {
@@ -22,19 +24,27 @@ mod test {
2224
//~^ NOTE lint level defined here
2325
//~^^ NOTE lint level defined here
2426
mod bad {
25-
fn CamelCase() {} //~ ERROR function `CamelCase` should have a snake case name
27+
fn CamelCase() {}
28+
//~^ ERROR function `CamelCase` should have a snake case name
29+
//~| NOTE #[forbid(bad_style)] implies #[forbid(non_snake_case)]
2630

27-
static bad: isize = 1; //~ ERROR static variable `bad` should have an upper case name
31+
static bad: isize = 1;
32+
//~^ ERROR static variable `bad` should have an upper case name
33+
//~| NOTE #[forbid(bad_style)] implies #[forbid(non_upper_case_globals)]
2834
}
2935

3036
mod warn {
3137
#![warn(bad_style)]
3238
//~^ NOTE lint level defined here
3339
//~| NOTE lint level defined here
3440

35-
fn CamelCase() {} //~ WARN function `CamelCase` should have a snake case name
41+
fn CamelCase() {}
42+
//~^ WARN function `CamelCase` should have a snake case name
43+
//~| NOTE #[warn(bad_style)] implies #[warn(non_snake_case)]
3644

37-
struct snake_case; //~ WARN type `snake_case` should have a camel case name
45+
struct snake_case;
46+
//~^ WARN type `snake_case` should have a camel case name
47+
//~| NOTE #[warn(bad_style)] implies #[warn(non_camel_case_types)]
3848
}
3949
}
4050

src/test/compile-fail/lint-output-format-2.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@
1111
// compile-flags: -F unused_features
1212
// aux-build:lint_output_format.rs
1313

14-
#![feature(foo)] //~ ERROR unused or unknown feature
14+
#![feature(foo)]
15+
//~^ ERROR unused or unknown feature
16+
//~| NOTE [-F unused-features]
1517

1618
#![feature(test_feature)]
1719

1820
extern crate lint_output_format;
1921
use lint_output_format::{foo, bar};
20-
//~^ WARNING use of deprecated item: text,
22+
//~^ WARNING use of deprecated item: text
23+
//~| NOTE #[warn(deprecated)] on by default
2124

2225
fn main() {
23-
let _x = foo(); //~ WARNING #[warn(deprecated)] on by default
26+
let _x = foo();
27+
//~^ WARNING use of deprecated item: text
28+
//~| NOTE #[warn(deprecated)] on by default
2429
let _y = bar();
2530
}

src/test/run-pass/path-lookahead.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
// Parser test for #37765
1212

13-
fn with_parens<T: ToString>(arg: T) -> String { //~WARN dead_code
14-
return (<T as ToString>::to_string(&arg)); //~WARN unused_parens
13+
fn with_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `with_parens`
14+
return (<T as ToString>::to_string(&arg)); //~WARN unnecessary parentheses around `return` value
1515
}
1616

17-
fn no_parens<T: ToString>(arg: T) -> String { //~WARN dead_code
17+
fn no_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `no_parens`
1818
return <T as ToString>::to_string(&arg);
1919
}
2020

src/test/ui/compare-method/proj-outlives-region.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0276]: impl has stricter requirements than trait, #[deny(extra_requirement_in_impl)] on by default
1+
error[E0276]: impl has stricter requirements than trait
22
--> $DIR/proj-outlives-region.rs:22:5
33
|
44
17 | fn foo() where T: 'a;
@@ -7,6 +7,7 @@ error[E0276]: impl has stricter requirements than trait, #[deny(extra_requiremen
77
22 | fn foo() where U: 'a { } //~ ERROR E0276
88
| ^^^^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `U: 'a`
99
|
10+
= note: #[deny(extra_requirement_in_impl)] on by default
1011
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1112
= note: for more information, see issue #37166 <https://github.com/rust-lang/rust/issues/37166>
1213

src/test/ui/compare-method/region-unrelated.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0276]: impl has stricter requirements than trait, #[deny(extra_requirement_in_impl)] on by default
1+
error[E0276]: impl has stricter requirements than trait
22
--> $DIR/region-unrelated.rs:22:5
33
|
44
17 | fn foo() where T: 'a;
@@ -7,6 +7,7 @@ error[E0276]: impl has stricter requirements than trait, #[deny(extra_requiremen
77
22 | fn foo() where V: 'a { }
88
| ^^^^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `V: 'a`
99
|
10+
= note: #[deny(extra_requirement_in_impl)] on by default
1011
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1112
= note: for more information, see issue #37166 <https://github.com/rust-lang/rust/issues/37166>
1213

src/test/ui/span/issue-24690.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: variable `theTwo` should have a snake case name such as `the_two`
44
19 | let theTwo = 2;
55
| ^^^^^^
66
|
7+
= note: #[deny(warnings)] implies #[deny(non_snake_case)]
78
note: lint level defined here
89
--> $DIR/issue-24690.rs:16:9
910
|
@@ -15,13 +16,16 @@ error: variable `theOtherTwo` should have a snake case name such as `the_other_t
1516
|
1617
20 | let theOtherTwo = 2;
1718
| ^^^^^^^^^^^
19+
|
20+
= note: #[deny(warnings)] implies #[deny(non_snake_case)]
1821

1922
error: unused variable: `theOtherTwo`
2023
--> $DIR/issue-24690.rs:20:9
2124
|
2225
20 | let theOtherTwo = 2;
2326
| ^^^^^^^^^^^
2427
|
28+
= note: #[deny(warnings)] implies #[deny(unused_variables)]
2529
note: lint level defined here
2630
--> $DIR/issue-24690.rs:16:9
2731
|
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
warning: unused imports: `Eq`, `Ord`, `PartialEq`, `PartialOrd`, #[warn(unused_imports)] on by default
1+
warning: unused imports: `Eq`, `Ord`, `PartialEq`, `PartialOrd`
22
--> $DIR/multispan-import-lint.rs:11:16
33
|
44
11 | use std::cmp::{Eq, Ord, min, PartialEq, PartialOrd};
55
| ^^ ^^^ ^^^^^^^^^ ^^^^^^^^^^
6+
|
7+
= note: #[warn(unused_imports)] on by default
68

0 commit comments

Comments
 (0)