Skip to content

Commit 9b2aacf

Browse files
authored
Rollup merge of #41722 - F001:warnTilde, r=petrochenkov
Suggest `!` for bitwise negation when encountering a `~` Fix #41679 Here is a program ```rust fn main() { let x = ~1; } ``` It's output: ``` error: `~` can not be used as an unary operator --> /home/fcc/temp/test.rs:4:13 | 4 | let x = ~1; | ^^ | = help: use `!` instead of `~` if you meant to bitwise negation ``` cc @bstrie
2 parents ecd7b48 + a9d3b34 commit 9b2aacf

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/libsyntax/parse/parser.rs

+13
Original file line numberDiff line numberDiff line change
@@ -2700,6 +2700,19 @@ impl<'a> Parser<'a> {
27002700
let (span, e) = self.interpolated_or_expr_span(e)?;
27012701
(span, self.mk_unary(UnOp::Not, e))
27022702
}
2703+
// Suggest `!` for bitwise negation when encountering a `~`
2704+
token::Tilde => {
2705+
self.bump();
2706+
let e = self.parse_prefix_expr(None);
2707+
let (span, e) = self.interpolated_or_expr_span(e)?;
2708+
let span_of_tilde = lo;
2709+
let mut err = self.diagnostic().struct_span_err(span_of_tilde,
2710+
"`~` can not be used as an unary operator");
2711+
err.span_label(span_of_tilde, &"did you mean `!`?");
2712+
err.help("use `!` instead of `~` if you meant to perform bitwise negation");
2713+
err.emit();
2714+
(span, self.mk_unary(UnOp::Not, e))
2715+
}
27032716
token::BinOp(token::Minus) => {
27042717
self.bump();
27052718
let e = self.parse_prefix_expr(None);
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 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 = ~1;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: `~` can not be used as an unary operator
2+
--> $DIR/issue-41679.rs:12:13
3+
|
4+
12 | let x = ~1;
5+
| ^ did you mean `!`?
6+
|
7+
= help: use `!` instead of `~` if you meant to perform bitwise negation
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)