Skip to content

Commit e6687f3

Browse files
oli-obkpietroalbini
authored andcommitted
Don't report deprecation lints in derive expansions
1 parent c02a36a commit e6687f3

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/librustc/lint/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,8 @@ pub fn provide(providers: &mut Providers<'_>) {
812812

813813
/// Returns whether `span` originates in a foreign crate's external macro.
814814
///
815-
/// This is used to test whether a lint should be entirely aborted above.
815+
/// This is used to test whether a lint should not even begin to figure out whether it should
816+
/// be reported on the current node.
816817
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
817818
let info = match span.ctxt().outer().expn_info() {
818819
Some(info) => info,
@@ -838,3 +839,17 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
838839
Err(_) => true,
839840
}
840841
}
842+
843+
/// Returns whether `span` originates in a derive macro's expansion
844+
pub fn in_derive_expansion(span: Span) -> bool {
845+
let info = match span.ctxt().outer().expn_info() {
846+
Some(info) => info,
847+
// no ExpnInfo means this span doesn't come from a macro
848+
None => return false,
849+
};
850+
851+
match info.format {
852+
ExpnFormat::MacroAttribute(symbol) => symbol.as_str().starts_with("derive("),
853+
_ => false,
854+
}
855+
}

src/librustc/middle/stability.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
pub use self::StabilityLevel::*;
55

6-
use crate::lint::{self, Lint};
6+
use crate::lint::{self, Lint, in_derive_expansion};
77
use crate::hir::{self, Item, Generics, StructField, Variant, HirId};
88
use crate::hir::def::Def;
99
use crate::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, LOCAL_CRATE};
@@ -571,6 +571,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
571571
suggestion: Option<Symbol>,
572572
message: &str,
573573
lint: &'static Lint| {
574+
if in_derive_expansion(span) {
575+
return;
576+
}
574577
let msg = if let Some(note) = note {
575578
format!("{}: {}", message, note)
576579
} else {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// compile-pass
2+
3+
#![deny(deprecated)]
4+
5+
#[deprecated = "oh no"]
6+
#[derive(Default)]
7+
struct X;
8+
9+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// compile-pass
2+
3+
#![forbid(deprecated)]
4+
5+
#[deprecated = "oh no"]
6+
#[derive(Default)]
7+
struct X;
8+
9+
fn main() {}

0 commit comments

Comments
 (0)