Skip to content
Closed
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
37 changes: 29 additions & 8 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4768,16 +4768,37 @@ impl<'a> Parser<'a> {
if self.eat(&token::Semi) {
stmt_span = stmt_span.with_hi(self.prev_span.hi());
}
let sugg = pprust::to_string(|s| {
use print::pprust::{PrintState, INDENT_UNIT};
s.ibox(INDENT_UNIT)?;
s.bopen()?;
s.print_stmt(&stmt)?;
s.bclose_maybe_open(stmt.span, INDENT_UNIT, false)
});

let (msg, sugg) = match self.sess.source_map()
.span_to_snippet(stmt_span)
.as_ref()
.map(|s| s.as_str()) {
Ok("and") => {
(
"try replacing this with",
"&&".to_string(),
)
},
Ok("or") => {
(
"try replacing this with",
"||".to_string(),
)
},
_ => (
"try placing this code inside a block",
pprust::to_string(|s| {
use print::pprust::{PrintState, INDENT_UNIT};
s.ibox(INDENT_UNIT)?;
s.bopen()?;
s.print_stmt(&stmt)?;
s.bclose_maybe_open(stmt.span, INDENT_UNIT, false)
}),
),
};
e.span_suggestion_with_applicability(
stmt_span,
"try placing this code inside a block",
msg,
sugg,
// speculative, has been misleading in the past (closed Issue #46836)
Applicability::MaybeIncorrect
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/if/if-with-and.rs
Original file line number Diff line number Diff line change
@@ -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 = 1;
let y = 2;
if x == 1 and y == 2 {}
//~^ NOTE this `if` statement has a condition, but no block
}
//~^ ERROR expected `{`, found `}`
10 changes: 10 additions & 0 deletions src/test/ui/if/if-with-and.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected `{`, found `and`
--> $DIR/if-with-and.rs:14:15
|
LL | if x == 1 and y == 2 {}
| -- ^^^ help: try replacing this with: `&&`
| |
| this `if` statement has a condition, but no block

error: aborting due to previous error

17 changes: 17 additions & 0 deletions src/test/ui/if/if-with-or.rs
Original file line number Diff line number Diff line change
@@ -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 = 1;
let y = 2;
if x == 1 or y == 2 {}
//~^ NOTE this `if` statement has a condition, but no block
}
//~^ ERROR expected `{`, found `}`
10 changes: 10 additions & 0 deletions src/test/ui/if/if-with-or.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected `{`, found `or`
--> $DIR/if-with-or.rs:14:15
|
LL | if x == 1 or y == 2 {}
| -- ^^ help: try replacing this with: `||`
| |
| this `if` statement has a condition, but no block

error: aborting due to previous error