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

Rewrite ... as ..= as a MachineApplicable 2018 idiom lint #55852

Merged
merged 2 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,9 +1020,12 @@ impl<'a> ast_visit::Visitor<'a> for EarlyContext<'a> {
}

fn visit_pat(&mut self, p: &'a ast::Pat) {
run_lints!(self, check_pat, p);
let mut visit_subpats = true;
run_lints!(self, check_pat, p, &mut visit_subpats);
self.check_id(p.id);
ast_visit::walk_pat(self, p);
if visit_subpats {
ast_visit::walk_pat(self, p);
}
}

fn visit_expr(&mut self, e: &'a ast::Expr) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ pub trait EarlyLintPass: LintPass {
fn check_block_post(&mut self, _: &EarlyContext<'_>, _: &ast::Block) { }
fn check_stmt(&mut self, _: &EarlyContext<'_>, _: &ast::Stmt) { }
fn check_arm(&mut self, _: &EarlyContext<'_>, _: &ast::Arm) { }
fn check_pat(&mut self, _: &EarlyContext<'_>, _: &ast::Pat) { }
fn check_pat(&mut self, _: &EarlyContext<'_>, _: &ast::Pat, _: &mut bool) { }
fn check_expr(&mut self, _: &EarlyContext<'_>, _: &ast::Expr) { }
fn check_expr_post(&mut self, _: &EarlyContext<'_>, _: &ast::Expr) { }
fn check_ty(&mut self, _: &EarlyContext<'_>, _: &ast::Ty) { }
Expand Down
44 changes: 34 additions & 10 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ use rustc::util::nodemap::FxHashSet;

use syntax::tokenstream::{TokenTree, TokenStream};
use syntax::ast;
use syntax::ptr::P;
use syntax::ast::Expr;
use syntax::attr;
use syntax::source_map::Spanned;
use syntax::edition::Edition;
use syntax::feature_gate::{AttributeGate, AttributeType, Stability, deprecated_attributes};
use syntax_pos::{BytePos, Span, SyntaxContext};
use syntax::symbol::keywords;
use syntax::errors::{Applicability, DiagnosticBuilder};
use syntax::print::pprust::expr_to_string;

use rustc::hir::{self, GenericParamKind, PatKind};
use rustc::hir::intravisit::FnKind;
Expand Down Expand Up @@ -1407,21 +1410,42 @@ impl LintPass for EllipsisInclusiveRangePatterns {
}

impl EarlyLintPass for EllipsisInclusiveRangePatterns {
fn check_pat(&mut self, cx: &EarlyContext, pat: &ast::Pat) {
use self::ast::{PatKind, RangeEnd, RangeSyntax};
fn check_pat(&mut self, cx: &EarlyContext, pat: &ast::Pat, visit_subpats: &mut bool) {
use self::ast::{PatKind, RangeEnd, RangeSyntax::DotDotDot};

/// If `pat` is a `...` pattern, return the start and end of the range, as well as the span
/// corresponding to the ellipsis.
fn matches_ellipsis_pat(pat: &ast::Pat) -> Option<(&P<Expr>, &P<Expr>, Span)> {
match &pat.node {
PatKind::Range(a, b, Spanned { span, node: RangeEnd::Included(DotDotDot), .. }) => {
Some((a, b, *span))
}
_ => None,
}
}

let (parenthesise, endpoints) = match &pat.node {
PatKind::Ref(subpat, _) => (true, matches_ellipsis_pat(&subpat)),
_ => (false, matches_ellipsis_pat(pat)),
};

if let PatKind::Range(
_, _, Spanned { span, node: RangeEnd::Included(RangeSyntax::DotDotDot) }
) = pat.node {
if let Some((start, end, join)) = endpoints {
let msg = "`...` range patterns are deprecated";
let suggestion = "use `..=` for an inclusive range";
let (span, replacement) = if parenthesise {
*visit_subpats = false;
(pat.span, format!("&({}..={})", expr_to_string(&start), expr_to_string(&end)))
} else {
(join, "..=".to_owned())
};
let mut err = cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, span, msg);
err.span_suggestion_short_with_applicability(
span, "use `..=` for an inclusive range", "..=".to_owned(),
// FIXME: outstanding problem with precedence in ref patterns:
// https://github.com/rust-lang/rust/issues/51043#issuecomment-392252285
Applicability::MaybeIncorrect
span,
suggestion,
replacement,
Applicability::MachineApplicable,
);
err.emit()
err.emit();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,12 @@ impl EarlyLintPass for UnusedParens {
self.check_unused_parens_expr(cx, &value, msg, followed_by_block);
}

fn check_pat(&mut self, cx: &EarlyContext, p: &ast::Pat) {
fn check_pat(&mut self, cx: &EarlyContext, p: &ast::Pat, _: &mut bool) {
use ast::PatKind::{Paren, Range};
// The lint visitor will visit each subpattern of `p`. We do not want to lint any range
// pattern no matter where it occurs in the pattern. For something like `&(a..=b)`, there
// is a recursive `check_pat` on `a` and `b`, but we will assume that if there are
// unnecessry parens they serve a purpose of readability.
// unnecessary parens they serve a purpose of readability.
if let Paren(ref pat) = p.node {
match pat.node {
Range(..) => {}
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/lint/inclusive-range-pattern-syntax.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ fn main() {
//~^ WARN `...` range patterns are deprecated
_ => {}
}

match &despondency {
&(1..=2) => {}
//~^ WARN `...` range patterns are deprecated
_ => {}
}
}
6 changes: 6 additions & 0 deletions src/test/ui/lint/inclusive-range-pattern-syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ fn main() {
//~^ WARN `...` range patterns are deprecated
_ => {}
}

match &despondency {
&1...2 => {}
//~^ WARN `...` range patterns are deprecated
_ => {}
}
}
6 changes: 6 additions & 0 deletions src/test/ui/lint/inclusive-range-pattern-syntax.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ note: lint level defined here
LL | #![warn(ellipsis_inclusive_range_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: `...` range patterns are deprecated
--> $DIR/inclusive-range-pattern-syntax.rs:25:9
|
LL | &1...2 => {}
| ^^^^^^ help: use `..=` for an inclusive range

4 changes: 2 additions & 2 deletions src/test/ui/range/range-inclusive-pattern-precedence.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ LL | box 10..=15 => {}
| ^^^^^^^ help: add parentheses to clarify the precedence: `(10 ..=15)`

warning: `...` range patterns are deprecated
--> $DIR/range-inclusive-pattern-precedence.rs:24:11
--> $DIR/range-inclusive-pattern-precedence.rs:24:9
|
LL | &0...9 => {}
| ^^^ help: use `..=` for an inclusive range
| ^^^^^^ help: use `..=` for an inclusive range
Copy link
Member

Choose a reason for hiding this comment

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

In the case where we recommend parentheses (and only then), I think we want to use span_suggestion_with_applicability (rather than span_suggestion_short_with_applicability), because that'll print the corrected code snippet after the span message (whereas the short variant methods omit it):

LL |         &0...9 => {}
   |         ^^^^^^ help: use `..=` for an inclusive range: &(0..=9)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, that's a good point.

|
note: lint level defined here
--> $DIR/range-inclusive-pattern-precedence.rs:19:9
Expand Down