From 6dc3a89085cef6953f1f4ed806c261c87a6977ae Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Thu, 13 Sep 2018 19:59:49 +0800 Subject: [PATCH 1/3] add ui test for and/or in if stmt --- src/test/ui/if/if-with-and.rs | 17 +++++++++++++++++ src/test/ui/if/if-with-or.rs | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/test/ui/if/if-with-and.rs create mode 100644 src/test/ui/if/if-with-or.rs diff --git a/src/test/ui/if/if-with-and.rs b/src/test/ui/if/if-with-and.rs new file mode 100644 index 0000000000000..9e0889033d30d --- /dev/null +++ b/src/test/ui/if/if-with-and.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 = 1; + let y = 2; + if x == 1 and y == 2 {} + //~^ NOTE this `if` statement has a condition, but no block +} +//~^ ERROR expected `{`, found `}` diff --git a/src/test/ui/if/if-with-or.rs b/src/test/ui/if/if-with-or.rs new file mode 100644 index 0000000000000..6480d293b4513 --- /dev/null +++ b/src/test/ui/if/if-with-or.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 = 1; + let y = 2; + if x == 1 or y == 2 {} + //~^ NOTE this `if` statement has a condition, but no block +} +//~^ ERROR expected `{`, found `}` From 393ce2120f69ecfaf26ec55c26a87024a4b90524 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Thu, 13 Sep 2018 20:25:39 +0800 Subject: [PATCH 2/3] lint to replace and/or with '&&/||` --- src/libsyntax/parse/parser.rs | 37 +++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 5f80af77f4940..65e963d702c7a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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 From 2cbec164f83ddfa556393fb676fd941b7afca855 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Thu, 13 Sep 2018 20:26:34 +0800 Subject: [PATCH 3/3] update ui stderr for and/or in if-stmt --- src/test/ui/if/if-with-and.stderr | 10 ++++++++++ src/test/ui/if/if-with-or.stderr | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 src/test/ui/if/if-with-and.stderr create mode 100644 src/test/ui/if/if-with-or.stderr diff --git a/src/test/ui/if/if-with-and.stderr b/src/test/ui/if/if-with-and.stderr new file mode 100644 index 0000000000000..d5ea012224239 --- /dev/null +++ b/src/test/ui/if/if-with-and.stderr @@ -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 + diff --git a/src/test/ui/if/if-with-or.stderr b/src/test/ui/if/if-with-or.stderr new file mode 100644 index 0000000000000..d47433bf9e19d --- /dev/null +++ b/src/test/ui/if/if-with-or.stderr @@ -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 +