Skip to content

Commit 4260c8b

Browse files
committedJul 20, 2018
Auto merge of #52467 - alexcrichton:lints-and-macros, r=Manishearth
Squash all lints tied to foreign macros by default This PR is a continuation of #49755 (thanks for the initial jump-start @Dylan-DPC!) and is targeted at solving #48855. This change updates the lint infrastructure to, by default, ignore all lints emitted for code that originates in a foreign macro. For example if `println!("...")` injects some idiomatic warnings these are all ignored by default. The rationale here is that for almost all lints there's no action that can be taken if the code originates from a foreign lint. Closes #48855 Closes #52483 Closes #52479
2 parents a5097f3 + 8adf08c commit 4260c8b

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed
 

‎src/librustc/lint/mod.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use lint::builtin::BuiltinLintDiagnostics;
4141
use session::{Session, DiagnosticMessageId};
4242
use std::hash;
4343
use syntax::ast;
44-
use syntax::codemap::MultiSpan;
44+
use syntax::codemap::{MultiSpan, ExpnFormat};
4545
use syntax::edition::Edition;
4646
use syntax::symbol::Symbol;
4747
use syntax::visit as ast_visit;
@@ -572,6 +572,22 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
572572
future_incompatible.reference);
573573
err.warn(&explanation);
574574
err.note(&citation);
575+
576+
// If this lint is *not* a future incompatibility warning then we want to be
577+
// sure to not be too noisy in some situations. If this code originates in a
578+
// foreign macro, aka something that this crate did not itself author, then
579+
// it's likely that there's nothing this crate can do about it. We probably
580+
// want to skip the lint entirely.
581+
//
582+
// For some lints though (like unreachable code) there's clear actionable
583+
// items to take care of (delete the macro invocation). As a result we have
584+
// a few lints we whitelist here for allowing a lint even though it's in a
585+
// foreign macro invocation.
586+
} else if lint_id != LintId::of(builtin::UNREACHABLE_CODE) &&
587+
lint_id != LintId::of(builtin::DEPRECATED) {
588+
if err.span.primary_spans().iter().any(|s| in_external_macro(sess, *s)) {
589+
err.cancel();
590+
}
575591
}
576592

577593
return err
@@ -673,3 +689,32 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'a, 'tcx> {
673689
pub fn provide(providers: &mut Providers) {
674690
providers.lint_levels = lint_levels;
675691
}
692+
693+
/// Returns whether `span` originates in a foreign crate's external macro.
694+
///
695+
/// This is used to test whether a lint should be entirely aborted above.
696+
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
697+
let info = match span.ctxt().outer().expn_info() {
698+
Some(info) => info,
699+
// no ExpnInfo means this span doesn't come from a macro
700+
None => return false,
701+
};
702+
703+
match info.format {
704+
ExpnFormat::MacroAttribute(..) => return true, // definitely a plugin
705+
ExpnFormat::CompilerDesugaring(_) => return true, // well, it's "external"
706+
ExpnFormat::MacroBang(..) => {} // check below
707+
}
708+
709+
let def_site = match info.def_site {
710+
Some(span) => span,
711+
// no span for the def_site means it's an external macro
712+
None => return true,
713+
};
714+
715+
match sess.codemap().span_to_snippet(def_site) {
716+
Ok(code) => !code.starts_with("macro_rules"),
717+
// no snippet = external macro or compiler-builtin expansion
718+
Err(_) => true,
719+
}
720+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[macro_export]
12+
macro_rules! bar {
13+
() => {use std::string::ToString;}
14+
}
15+
16+
#[macro_export]
17+
macro_rules! baz {
18+
($i:item) => ($i)
19+
}
20+
21+
#[macro_export]
22+
macro_rules! baz2 {
23+
($($i:tt)*) => ($($i)*)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:lints-in-foreign-macros.rs
12+
// compile-pass
13+
14+
#![warn(unused_imports)]
15+
16+
#[macro_use]
17+
extern crate lints_in_foreign_macros;
18+
19+
macro_rules! foo {
20+
() => {use std::string::ToString;} //~ WARN: unused import
21+
}
22+
23+
mod a { foo!(); }
24+
mod b { bar!(); }
25+
mod c { baz!(use std::string::ToString;); } //~ WARN: unused import
26+
mod d { baz2!(use std::string::ToString;); } //~ WARN: unused import
27+
28+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
warning: unused import: `std::string::ToString`
2+
--> $DIR/lints-in-foreign-macros.rs:20:16
3+
|
4+
LL | () => {use std::string::ToString;} //~ WARN: unused import
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
...
7+
LL | mod a { foo!(); }
8+
| ------- in this macro invocation
9+
|
10+
note: lint level defined here
11+
--> $DIR/lints-in-foreign-macros.rs:14:9
12+
|
13+
LL | #![warn(unused_imports)]
14+
| ^^^^^^^^^^^^^^
15+
16+
warning: unused import: `std::string::ToString`
17+
--> $DIR/lints-in-foreign-macros.rs:25:18
18+
|
19+
LL | mod c { baz!(use std::string::ToString;); } //~ WARN: unused import
20+
| ^^^^^^^^^^^^^^^^^^^^^
21+
22+
warning: unused import: `std::string::ToString`
23+
--> $DIR/lints-in-foreign-macros.rs:26:19
24+
|
25+
LL | mod d { baz2!(use std::string::ToString;); } //~ WARN: unused import
26+
| ^^^^^^^^^^^^^^^^^^^^^
27+

0 commit comments

Comments
 (0)