Skip to content

Commit 5f308ee

Browse files
committed
Auto merge of #48309 - mark-i-m:anon_param_lint, r=nikomatsakis
Make anon params lint warn-by-default This is intended as a followup on anonymous parameters deprecation. Cross-posting from #41686: > After having read a bit more of the discussion that I can find, I propose a more aggressive deprecation strategy: > - We make the lint warn-by-default as soon as possible > - We make anon parameters a hard error at the epoch boundary cc @matklad @est31 @aturon
2 parents ad2591c + 0e53b78 commit 5f308ee

16 files changed

+109
-22
lines changed

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#![feature(catch_expr)]
7070
#![feature(test)]
7171
#![feature(in_band_lifetimes)]
72+
#![feature(macro_at_most_once_rep)]
7273

7374
#![recursion_limit="512"]
7475

src/librustc/lint/mod.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ pub struct Lint {
7777
/// e.g. "imports that are never used"
7878
pub desc: &'static str,
7979

80-
/// Deny lint after this edition
81-
pub edition_deny: Option<Edition>,
80+
/// Starting at the given edition, default to the given lint level. If this is `None`, then use
81+
/// `default_level`.
82+
pub edition_lint_opts: Option<(Edition, Level)>,
8283
}
8384

8485
impl Lint {
@@ -88,41 +89,40 @@ impl Lint {
8889
}
8990

9091
pub fn default_level(&self, session: &Session) -> Level {
91-
if let Some(edition_deny) = self.edition_deny {
92-
if session.edition() >= edition_deny {
93-
return Level::Deny
94-
}
95-
}
96-
self.default_level
92+
self.edition_lint_opts
93+
.filter(|(e, _)| *e <= session.edition())
94+
.map(|(_, l)| l)
95+
.unwrap_or(self.default_level)
9796
}
9897
}
9998

10099
/// Declare a static item of type `&'static Lint`.
101100
#[macro_export]
102101
macro_rules! declare_lint {
103-
($vis: vis $NAME: ident, $Level: ident, $desc: expr, $edition: expr) => (
102+
($vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
104103
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
105104
name: stringify!($NAME),
106105
default_level: $crate::lint::$Level,
107106
desc: $desc,
108-
edition_deny: Some($edition)
107+
edition_lint_opts: None,
109108
};
110109
);
111-
($vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
110+
($vis: vis $NAME: ident, $Level: ident, $desc: expr,
111+
$lint_edition: expr => $edition_level: ident $(,)?
112+
) => (
112113
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
113114
name: stringify!($NAME),
114115
default_level: $crate::lint::$Level,
115116
desc: $desc,
116-
edition_deny: None,
117+
edition_lint_opts: Some(($lint_edition, $crate::lint::Level::$edition_level)),
117118
};
118119
);
119120
}
120121

121122
/// Declare a static `LintArray` and return it as an expression.
122123
#[macro_export]
123124
macro_rules! lint_array {
124-
($( $lint:expr ),*,) => { lint_array!( $( $lint ),* ) };
125-
($( $lint:expr ),*) => {{
125+
($( $lint:expr ),* $(,)?) => {{
126126
static ARRAY: LintArray = &[ $( &$lint ),* ];
127127
ARRAY
128128
}}

src/librustc_lint/builtin.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use std::collections::HashSet;
4343

4444
use syntax::ast;
4545
use syntax::attr;
46+
use syntax::edition::Edition;
4647
use syntax::feature_gate::{AttributeGate, AttributeType, Stability, deprecated_attributes};
4748
use syntax_pos::{BytePos, Span, SyntaxContext};
4849
use syntax::symbol::keywords;
@@ -616,7 +617,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations {
616617
declare_lint! {
617618
pub ANONYMOUS_PARAMETERS,
618619
Allow,
619-
"detects anonymous parameters"
620+
"detects anonymous parameters",
621+
Edition::Edition2018 => Warn,
620622
}
621623

622624
/// Checks for use of anonymous parameters (RFC 1685)
@@ -637,9 +639,29 @@ impl EarlyLintPass for AnonymousParameters {
637639
match arg.pat.node {
638640
ast::PatKind::Ident(_, ident, None) => {
639641
if ident.name == keywords::Invalid.name() {
640-
cx.span_lint(ANONYMOUS_PARAMETERS,
641-
arg.pat.span,
642-
"use of deprecated anonymous parameter");
642+
let ty_snip = cx
643+
.sess
644+
.codemap()
645+
.span_to_snippet(arg.ty.span);
646+
647+
let (ty_snip, appl) = if let Ok(snip) = ty_snip {
648+
(snip, Applicability::MachineApplicable)
649+
} else {
650+
("<type>".to_owned(), Applicability::HasPlaceholders)
651+
};
652+
653+
cx.struct_span_lint(
654+
ANONYMOUS_PARAMETERS,
655+
arg.pat.span,
656+
"anonymous parameters are deprecated and will be \
657+
removed in the next edition."
658+
).span_suggestion_with_applicability(
659+
arg.pat.span,
660+
"Try naming the parameter or explicitly \
661+
ignoring it",
662+
format!("_: {}", ty_snip),
663+
appl
664+
).emit();
643665
}
644666
}
645667
_ => (),

src/librustc_lint/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(macro_vis_matcher)]
3030
#![feature(quote)]
3131
#![feature(rustc_diagnostic_macros)]
32+
#![feature(macro_at_most_once_rep)]
3233

3334
extern crate syntax;
3435
#[macro_use]

src/test/compile-fail-fulldeps/auxiliary/lint_for_crate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(plugin_registrar, rustc_private)]
1414
#![feature(box_syntax)]
1515
#![feature(macro_vis_matcher)]
16+
#![feature(macro_at_most_once_rep)]
1617

1718
#[macro_use] extern crate rustc;
1819
extern crate rustc_plugin;

src/test/compile-fail-fulldeps/auxiliary/lint_group_plugin_test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(plugin_registrar)]
1414
#![feature(box_syntax, rustc_private)]
1515
#![feature(macro_vis_matcher)]
16+
#![feature(macro_at_most_once_rep)]
1617

1718
// Load rustc as a plugin to get macros
1819
#[macro_use]

src/test/compile-fail-fulldeps/auxiliary/lint_plugin_test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(plugin_registrar)]
1414
#![feature(box_syntax, rustc_private)]
1515
#![feature(macro_vis_matcher)]
16+
#![feature(macro_at_most_once_rep)]
1617

1718
extern crate syntax;
1819

src/test/compile-fail/anon-params-deprecated.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
// Test for the anonymous_parameters deprecation lint (RFC 1685)
1313

1414
trait T {
15-
fn foo(i32); //~ ERROR use of deprecated anonymous parameter
15+
fn foo(i32); //~ ERROR anonymous parameters are deprecated
1616
//~| WARNING hard error
1717

1818
fn bar_with_default_impl(String, String) {}
19-
//~^ ERROR use of deprecated anonymous parameter
19+
//~^ ERROR anonymous parameters are deprecated
2020
//~| WARNING hard error
21-
//~| ERROR use of deprecated anonymous parameter
21+
//~| ERROR anonymous parameters are deprecated
2222
//~| WARNING hard error
2323
}
2424

src/test/compile-fail/future-incompatible-lint-group.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![deny(future_incompatible)]
1212

1313
trait Tr {
14-
fn f(u8) {} //~ ERROR use of deprecated anonymous parameter
14+
fn f(u8) {} //~ ERROR anonymous parameters are deprecated
1515
//~^ WARN this was previously accepted
1616
}
1717

src/test/run-pass-fulldeps/auxiliary/lint_for_crate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(plugin_registrar, rustc_private)]
1414
#![feature(box_syntax)]
1515
#![feature(macro_vis_matcher)]
16+
#![feature(macro_at_most_once_rep)]
1617

1718
#[macro_use] extern crate rustc;
1819
extern crate rustc_plugin;

src/test/run-pass-fulldeps/proc-macro/auxiliary/issue-40001-plugin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010
#![feature(box_syntax, plugin, plugin_registrar, rustc_private)]
1111
#![feature(macro_vis_matcher)]
12+
#![feature(macro_at_most_once_rep)]
1213
#![crate_type = "dylib"]
1314

1415
#[macro_use]

src/test/ui-fulldeps/auxiliary/lint_group_plugin_test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(plugin_registrar)]
1414
#![feature(box_syntax, rustc_private)]
1515
#![feature(macro_vis_matcher)]
16+
#![feature(macro_at_most_once_rep)]
1617

1718
// Load rustc as a plugin to get macros
1819
#[macro_use]

src/test/ui-fulldeps/auxiliary/lint_plugin_test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(plugin_registrar)]
1414
#![feature(box_syntax, rustc_private)]
1515
#![feature(macro_vis_matcher)]
16+
#![feature(macro_at_most_once_rep)]
1617

1718
extern crate syntax;
1819

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2016 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+
// tests that the anonymous_parameters lint is warn-by-default on the 2018 edition
12+
13+
// compile-pass
14+
// compile-flags: --edition=2018
15+
// run-rustfix
16+
17+
trait Foo {
18+
fn foo(_: u8);
19+
//^ WARN anonymous parameters are deprecated
20+
//| WARN this was previously accepted
21+
}
22+
23+
fn main() {}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2016 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+
// tests that the anonymous_parameters lint is warn-by-default on the 2018 edition
12+
13+
// compile-pass
14+
// compile-flags: --edition=2018
15+
// run-rustfix
16+
17+
trait Foo {
18+
fn foo(u8);
19+
//^ WARN anonymous parameters are deprecated
20+
//| WARN this was previously accepted
21+
}
22+
23+
fn main() {}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: anonymous parameters are deprecated and will be removed in the next edition.
2+
--> $DIR/lint-anon-param-edition.rs:18:12
3+
|
4+
LL | fn foo(u8);
5+
| ^^ help: Try naming the parameter or explicitly ignoring it: `_: u8`
6+
|
7+
= note: #[warn(anonymous_parameters)] on by default
8+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9+
= note: for more information, see issue #41686 <https://github.com/rust-lang/rust/issues/41686>
10+

0 commit comments

Comments
 (0)