From 23d59d00be7c12a4b8fef68ef663a6d97290c415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 28 Jun 2018 14:58:54 -0700 Subject: [PATCH] Suggest correct comparison against negative literal When parsing as emplacement syntax (`x<-1`), suggest the correct syntax for comparison against a negative value (`x< -1`). --- src/librustc_passes/ast_validation.rs | 27 ++++++++++++++----- src/libsyntax/ast.rs | 10 +++++++ src/test/ui/suggestions/placement-syntax.rs | 17 ++++++++++++ .../ui/suggestions/placement-syntax.stderr | 14 ++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 src/test/ui/suggestions/placement-syntax.rs create mode 100644 src/test/ui/suggestions/placement-syntax.stderr diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index 25187032fb4..00b71accbf5 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -172,12 +172,27 @@ impl<'a> Visitor<'a> for AstValidator<'a> { ExprKind::InlineAsm(..) if !self.session.target.target.options.allow_asm => { span_err!(self.session, expr.span, E0472, "asm! is unsupported on this target"); } - ExprKind::ObsoleteInPlace(..) => { - self.err_handler() - .struct_span_err(expr.span, "emplacement syntax is obsolete (for now, anyway)") - .note("for more information, see \ - ") - .emit(); + ExprKind::ObsoleteInPlace(ref place, ref val) => { + let mut err = self.err_handler().struct_span_err( + expr.span, + "emplacement syntax is obsolete (for now, anyway)", + ); + err.note( + "for more information, see \ + " + ); + match val.node { + ExprKind::Lit(ref v) if v.node.is_numeric() => { + err.span_suggestion( + place.span.between(val.span), + "if you meant to write a comparison against a negative value, add a \ + space in between `<` and `-`", + "< -".to_string(), + ); + } + _ => {} + } + err.emit(); } _ => {} } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 53465c071f3..07e22b02c55 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1298,6 +1298,16 @@ impl LitKind { } } + /// Returns true if this is a numeric literal. + pub fn is_numeric(&self) -> bool { + match *self { + LitKind::Int(..) | + LitKind::Float(..) | + LitKind::FloatUnsuffixed(..) => true, + _ => false, + } + } + /// Returns true if this literal has no suffix. Note: this will return true /// for literals with prefixes such as raw strings and byte strings. pub fn is_unsuffixed(&self) -> bool { diff --git a/src/test/ui/suggestions/placement-syntax.rs b/src/test/ui/suggestions/placement-syntax.rs new file mode 100644 index 00000000000..39252597a23 --- /dev/null +++ b/src/test/ui/suggestions/placement-syntax.rs @@ -0,0 +1,17 @@ +// Copyright 2018 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let x = -5; + if x<-1 { + //~^ ERROR emplacement syntax is obsolete + println!("ok"); + } +} diff --git a/src/test/ui/suggestions/placement-syntax.stderr b/src/test/ui/suggestions/placement-syntax.stderr new file mode 100644 index 00000000000..933ba96519c --- /dev/null +++ b/src/test/ui/suggestions/placement-syntax.stderr @@ -0,0 +1,14 @@ +error: emplacement syntax is obsolete (for now, anyway) + --> $DIR/placement-syntax.rs:13:8 + | +LL | if x<-1 { + | ^^^^ + | + = note: for more information, see +help: if you meant to write a comparison against a negative value, add a space in between `<` and `-` + | +LL | if x< -1 { + | ^^^ + +error: aborting due to previous error +