Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make anon params lint warn-by-default #48309

Merged
merged 1 commit into from
May 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#![feature(catch_expr)]
#![feature(test)]
#![feature(in_band_lifetimes)]
#![feature(macro_at_most_once_rep)]

#![recursion_limit="512"]

Expand Down
28 changes: 14 additions & 14 deletions src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ pub struct Lint {
/// e.g. "imports that are never used"
pub desc: &'static str,

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

impl Lint {
Expand All @@ -88,41 +89,40 @@ impl Lint {
}

pub fn default_level(&self, session: &Session) -> Level {
if let Some(edition_deny) = self.edition_deny {
if session.edition() >= edition_deny {
return Level::Deny
}
}
self.default_level
self.edition_lint_opts
.filter(|(e, _)| *e <= session.edition())
.map(|(_, l)| l)
.unwrap_or(self.default_level)
}
}

/// Declare a static item of type `&'static Lint`.
#[macro_export]
macro_rules! declare_lint {
($vis: vis $NAME: ident, $Level: ident, $desc: expr, $edition: expr) => (
($vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
name: stringify!($NAME),
default_level: $crate::lint::$Level,
desc: $desc,
edition_deny: Some($edition)
edition_lint_opts: None,
};
);
($vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
($vis: vis $NAME: ident, $Level: ident, $desc: expr,
$lint_edition: expr => $edition_level: ident $(,)?
) => (
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
name: stringify!($NAME),
default_level: $crate::lint::$Level,
desc: $desc,
edition_deny: None,
edition_lint_opts: Some(($lint_edition, $crate::lint::Level::$edition_level)),
};
);
}

/// Declare a static `LintArray` and return it as an expression.
#[macro_export]
macro_rules! lint_array {
($( $lint:expr ),*,) => { lint_array!( $( $lint ),* ) };
($( $lint:expr ),*) => {{
($( $lint:expr ),* $(,)?) => {{
static ARRAY: LintArray = &[ $( &$lint ),* ];
ARRAY
}}
Expand Down
30 changes: 26 additions & 4 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use std::collections::HashSet;

use syntax::ast;
use syntax::attr;
use syntax::edition::Edition;
use syntax::feature_gate::{AttributeGate, AttributeType, Stability, deprecated_attributes};
use syntax_pos::{BytePos, Span, SyntaxContext};
use syntax::symbol::keywords;
Expand Down Expand Up @@ -616,7 +617,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations {
declare_lint! {
pub ANONYMOUS_PARAMETERS,
Allow,
"detects anonymous parameters"
"detects anonymous parameters",
Edition::Edition2018 => Warn,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty

}

/// Checks for use of anonymous parameters (RFC 1685)
Expand All @@ -637,9 +639,29 @@ impl EarlyLintPass for AnonymousParameters {
match arg.pat.node {
ast::PatKind::Ident(_, ident, None) => {
if ident.name == keywords::Invalid.name() {
cx.span_lint(ANONYMOUS_PARAMETERS,
arg.pat.span,
"use of deprecated anonymous parameter");
let ty_snip = cx
.sess
.codemap()
.span_to_snippet(arg.ty.span);

let (ty_snip, appl) = if let Ok(snip) = ty_snip {
(snip, Applicability::MachineApplicable)
} else {
("<type>".to_owned(), Applicability::HasPlaceholders)
};

cx.struct_span_lint(
ANONYMOUS_PARAMETERS,
arg.pat.span,
"anonymous parameters are deprecated and will be \
removed in the next edition."
).span_suggestion_with_applicability(
arg.pat.span,
"Try naming the parameter or explicitly \
ignoring it",
format!("_: {}", ty_snip),
appl
).emit();
}
}
_ => (),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#![feature(macro_vis_matcher)]
#![feature(quote)]
#![feature(rustc_diagnostic_macros)]
#![feature(macro_at_most_once_rep)]

extern crate syntax;
#[macro_use]
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail-fulldeps/auxiliary/lint_for_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![feature(plugin_registrar, rustc_private)]
#![feature(box_syntax)]
#![feature(macro_vis_matcher)]
#![feature(macro_at_most_once_rep)]

#[macro_use] extern crate rustc;
extern crate rustc_plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![feature(plugin_registrar)]
#![feature(box_syntax, rustc_private)]
#![feature(macro_vis_matcher)]
#![feature(macro_at_most_once_rep)]

// Load rustc as a plugin to get macros
#[macro_use]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![feature(plugin_registrar)]
#![feature(box_syntax, rustc_private)]
#![feature(macro_vis_matcher)]
#![feature(macro_at_most_once_rep)]

extern crate syntax;

Expand Down
6 changes: 3 additions & 3 deletions src/test/compile-fail/anon-params-deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// Test for the anonymous_parameters deprecation lint (RFC 1685)

trait T {
fn foo(i32); //~ ERROR use of deprecated anonymous parameter
fn foo(i32); //~ ERROR anonymous parameters are deprecated
//~| WARNING hard error

fn bar_with_default_impl(String, String) {}
//~^ ERROR use of deprecated anonymous parameter
//~^ ERROR anonymous parameters are deprecated
//~| WARNING hard error
//~| ERROR use of deprecated anonymous parameter
//~| ERROR anonymous parameters are deprecated
//~| WARNING hard error
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/future-incompatible-lint-group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![deny(future_incompatible)]

trait Tr {
fn f(u8) {} //~ ERROR use of deprecated anonymous parameter
fn f(u8) {} //~ ERROR anonymous parameters are deprecated
//~^ WARN this was previously accepted
}

Expand Down
1 change: 1 addition & 0 deletions src/test/run-pass-fulldeps/auxiliary/lint_for_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![feature(plugin_registrar, rustc_private)]
#![feature(box_syntax)]
#![feature(macro_vis_matcher)]
#![feature(macro_at_most_once_rep)]

#[macro_use] extern crate rustc;
extern crate rustc_plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.
#![feature(box_syntax, plugin, plugin_registrar, rustc_private)]
#![feature(macro_vis_matcher)]
#![feature(macro_at_most_once_rep)]
#![crate_type = "dylib"]

#[macro_use]
Expand Down
1 change: 1 addition & 0 deletions src/test/ui-fulldeps/auxiliary/lint_group_plugin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![feature(plugin_registrar)]
#![feature(box_syntax, rustc_private)]
#![feature(macro_vis_matcher)]
#![feature(macro_at_most_once_rep)]

// Load rustc as a plugin to get macros
#[macro_use]
Expand Down
1 change: 1 addition & 0 deletions src/test/ui-fulldeps/auxiliary/lint_plugin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![feature(plugin_registrar)]
#![feature(box_syntax, rustc_private)]
#![feature(macro_vis_matcher)]
#![feature(macro_at_most_once_rep)]

extern crate syntax;

Expand Down
23 changes: 23 additions & 0 deletions src/test/ui/lint-anon-param-edition.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// tests that the anonymous_parameters lint is warn-by-default on the 2018 edition

// compile-pass
// compile-flags: --edition=2018
// run-rustfix

trait Foo {
fn foo(_: u8);
//^ WARN anonymous parameters are deprecated
//| WARN this was previously accepted
}

fn main() {}
23 changes: 23 additions & 0 deletions src/test/ui/lint-anon-param-edition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// tests that the anonymous_parameters lint is warn-by-default on the 2018 edition

// compile-pass
// compile-flags: --edition=2018
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last thing, @mark-i-m! We now have rustfix test support. If you add // run-rustfix here, it will actually try applying your suggestion. The result will appear in a lint-anon-param-edition.fixed file (you might need to run ./x.py test with the --bless option to auto-create that file though).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh lol! That does not work correctly:

fn foo_: u8u8);

Any ideas what went wrong? Am I using the wrong Span or something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you paste the rustc output of running your branch on this file? Preferably also the json output.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: anonymous parameters are deprecated and will be removed in the next edition.
  --> /nobackup/rust2/src/test/ui/lint-anon-param-edition.rs:18:11
   |
LL |     fn foo(u8);
   |           ^ help: Try naming the parameter or explicitly ignoring it: `_: u8`
   |
   = note: #[warn(anonymous_parameters)] on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #41686 <https://github.com/rust-lang/rust/issues/41686>
{
  "message": "anonymous parameters are deprecated and will be removed in the next edition.",
  "code": {
    "code": "anonymous_parameters",
    "explanation": null
  },
  "level": "warning",
  "spans": [
    {
      "file_name": "\/nobackup\/rust2\/src\/test\/ui\/lint-anon-param-edition.rs",
      "byte_start": 638,
      "byte_end": 639,
      "line_start": 18,
      "line_end": 18,
      "column_start": 11,
      "column_end": 12,
      "is_primary": true,
      "text": [
        {
          "text": "    fn foo(u8);",
          "highlight_start": 11,
          "highlight_end": 12
        }
      ],
      "label": null,
      "suggested_replacement": null,
      "expansion": null
    }
  ],
  "children": [
    {
      "message": "#[warn(anonymous_parameters)] on by default",
      "code": null,
      "level": "note",
      "spans": [
        
      ],
      "children": [
        
      ],
      "rendered": null
    },
    {
      "message": "this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!",
      "code": null,
      "level": "warning",
      "spans": [
        
      ],
      "children": [
        
      ],
      "rendered": null
    },
    {
      "message": "for more information, see issue #41686 <https:\/\/github.com\/rust-lang\/rust\/issues\/41686>",
      "code": null,
      "level": "note",
      "spans": [
        
      ],
      "children": [
        
      ],
      "rendered": null
    },
    {
      "message": "Try naming the parameter or explicitly ignoring it",
      "code": null,
      "level": "help",
      "spans": [
        {
          "file_name": "\/nobackup\/rust2\/src\/test\/ui\/lint-anon-param-edition.rs",
          "byte_start": 638,
          "byte_end": 639,
          "line_start": 18,
          "line_end": 18,
          "column_start": 11,
          "column_end": 12,
          "is_primary": true,
          "text": [
            {
              "text": "    fn foo(u8);",
              "highlight_start": 11,
              "highlight_end": 12
            }
          ],
          "label": null,
          "suggested_replacement": "_: u8",
          "expansion": null
        }
      ],
      "children": [
        
      ],
      "rendered": null
    }
  ],
  "rendered": "warning: anonymous parameters are deprecated and will be removed in the next edition.\n  --> \/nobackup\/rust2\/src\/test\/ui\/lint-anon-param-edition.rs:18:11\n   |\nLL |     fn foo(u8);\n   |           ^ help: Try naming the parameter or explicitly ignoring it: `_: u8`\n   |\n   = note: #[warn(anonymous_parameters)] on by default\n   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!\n   = note: for more information, see issue #41686 <https:\/\/github.com\/rust-lang\/rust\/issues\/41686>\n\n"
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a parsing bug with the span? I.e. not your fault.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Manishearth I was wondering if this was because I used arg.pat.span as a the span for the suggestion. Since there is no pat (the arg is anonymous), maybe it just returns the span of the (? Should I use arg.ty.span instead?

// run-rustfix

trait Foo {
fn foo(u8);
//^ WARN anonymous parameters are deprecated
//| WARN this was previously accepted
}

fn main() {}
10 changes: 10 additions & 0 deletions src/test/ui/lint-anon-param-edition.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: anonymous parameters are deprecated and will be removed in the next edition.
--> $DIR/lint-anon-param-edition.rs:18:12
|
LL | fn foo(u8);
| ^^ help: Try naming the parameter or explicitly ignoring it: `_: u8`
|
= note: #[warn(anonymous_parameters)] on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41686 <https://github.com/rust-lang/rust/issues/41686>