Skip to content

Commit

Permalink
Suggest correct comparison against negative literal
Browse files Browse the repository at this point in the history
When parsing as emplacement syntax (`x<-1`), suggest the correct syntax
for comparison against a negative value (`x< -1`).
  • Loading branch information
estebank committed Jun 28, 2018
1 parent 5d95db3 commit 23d59d0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/librustc_passes/ast_validation.rs
Expand Up @@ -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 \
<https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911>")
.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 \
<https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911>"
);
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();
}
_ => {}
}
Expand Down
10 changes: 10 additions & 0 deletions src/libsyntax/ast.rs
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions 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 <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.

fn main() {
let x = -5;
if x<-1 {
//~^ ERROR emplacement syntax is obsolete
println!("ok");
}
}
14 changes: 14 additions & 0 deletions 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 <https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911>
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

0 comments on commit 23d59d0

Please sign in to comment.