Skip to content

Commit 65ec6c6

Browse files
authored
Rollup merge of rust-lang#51883 - estebank:placement-suggestion, r=varkor
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`). Fix rust-lang#45651.
2 parents 1991a9b + 23d59d0 commit 65ec6c6

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

src/librustc_passes/ast_validation.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,27 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
172172
ExprKind::InlineAsm(..) if !self.session.target.target.options.allow_asm => {
173173
span_err!(self.session, expr.span, E0472, "asm! is unsupported on this target");
174174
}
175-
ExprKind::ObsoleteInPlace(..) => {
176-
self.err_handler()
177-
.struct_span_err(expr.span, "emplacement syntax is obsolete (for now, anyway)")
178-
.note("for more information, see \
179-
<https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911>")
180-
.emit();
175+
ExprKind::ObsoleteInPlace(ref place, ref val) => {
176+
let mut err = self.err_handler().struct_span_err(
177+
expr.span,
178+
"emplacement syntax is obsolete (for now, anyway)",
179+
);
180+
err.note(
181+
"for more information, see \
182+
<https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911>"
183+
);
184+
match val.node {
185+
ExprKind::Lit(ref v) if v.node.is_numeric() => {
186+
err.span_suggestion(
187+
place.span.between(val.span),
188+
"if you meant to write a comparison against a negative value, add a \
189+
space in between `<` and `-`",
190+
"< -".to_string(),
191+
);
192+
}
193+
_ => {}
194+
}
195+
err.emit();
181196
}
182197
_ => {}
183198
}

src/libsyntax/ast.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,16 @@ impl LitKind {
12981298
}
12991299
}
13001300

1301+
/// Returns true if this is a numeric literal.
1302+
pub fn is_numeric(&self) -> bool {
1303+
match *self {
1304+
LitKind::Int(..) |
1305+
LitKind::Float(..) |
1306+
LitKind::FloatUnsuffixed(..) => true,
1307+
_ => false,
1308+
}
1309+
}
1310+
13011311
/// Returns true if this literal has no suffix. Note: this will return true
13021312
/// for literals with prefixes such as raw strings and byte strings.
13031313
pub fn is_unsuffixed(&self) -> bool {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 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+
fn main() {
12+
let x = -5;
13+
if x<-1 {
14+
//~^ ERROR emplacement syntax is obsolete
15+
println!("ok");
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: emplacement syntax is obsolete (for now, anyway)
2+
--> $DIR/placement-syntax.rs:13:8
3+
|
4+
LL | if x<-1 {
5+
| ^^^^
6+
|
7+
= note: for more information, see <https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911>
8+
help: if you meant to write a comparison against a negative value, add a space in between `<` and `-`
9+
|
10+
LL | if x< -1 {
11+
| ^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)