File tree 3 files changed +36
-0
lines changed
3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -2700,6 +2700,19 @@ impl<'a> Parser<'a> {
2700
2700
let ( span, e) = self . interpolated_or_expr_span ( e) ?;
2701
2701
( span, self . mk_unary ( UnOp :: Not , e) )
2702
2702
}
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
+ }
2703
2716
token:: BinOp ( token:: Minus ) => {
2704
2717
self . bump ( ) ;
2705
2718
let e = self . parse_prefix_expr ( None ) ;
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments