-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Give correct suggestion for a typo in raw pointers #145903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -276,6 +276,34 @@ impl<'a> Parser<'a> { | |||||||
) -> PResult<'a, Box<Ty>> { | ||||||||
let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes; | ||||||||
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery); | ||||||||
|
||||||||
if (self.token.is_keyword(kw::Const) || self.token.is_keyword(kw::Mut)) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably should also be moved into a separate function and also moved way down into the actually |
||||||||
&& self.look_ahead(1, |t| *t == token::Star) | ||||||||
{ | ||||||||
let kw_span = self.token.span; | ||||||||
self.bump(); | ||||||||
|
||||||||
if self.eat(exp!(Star)) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should try to parse a subsequent type via a snapshot. Or at least check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This eat is always true, since we checked the lookahead above |
||||||||
let star_span = self.prev_token.span; | ||||||||
|
||||||||
let _ = self.parse_ty_no_question_mark_recover(); | ||||||||
|
||||||||
let guar = self | ||||||||
.dcx() | ||||||||
.struct_span_err( | ||||||||
kw_span, | ||||||||
"raw pointer type must have a star before the mutability keyword", | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I find the phrasing kind of awkward -- "must have a star before the mutability keyword" doesn't really point to the fact that there is a star, and it's just misplaced. And also I wouldn't call " I'm not sure if what I suggested is any better, but I wonder if there's a way to suggest the change here that sounds more descriptive. |
||||||||
) | ||||||||
.with_multipart_suggestion( | ||||||||
"move it to the other side", | ||||||||
vec![(star_span, String::new()), (kw_span.shrink_to_lo(), "*".to_string())], | ||||||||
Applicability::MachineApplicable, | ||||||||
) | ||||||||
.emit(); | ||||||||
|
||||||||
return Ok(self.mk_ty(kw_span.to(star_span), TyKind::Err(guar))); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not recover this like a |
||||||||
} | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) { | ||||||||
let attrs_wrapper = self.parse_outer_attributes()?; | ||||||||
let raw_attrs = attrs_wrapper.take_for_recovery(self.psess); | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
//@ run-rustfix | ||
|
||
#![allow(unused)] | ||
|
||
pub const P1: *const u8 = 0 as _; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P2: *mut u8 = 1 as _; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P3: *const i32 = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P4: *const i32 = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P5: *mut i32 = std::ptr::null_mut(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P6: *mut i32 = std::ptr::null_mut(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P7: *const Vec<u8> = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P8: *const std::collections::HashMap<String, i32> = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
fn func1(p: *const u8) {} | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
fn func2(p: *mut u8) {} | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
fn func3() -> *const u8 { std::ptr::null() } | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
fn func4() -> *mut u8 { std::ptr::null_mut() } | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
struct S1 { | ||
field: *const u8, | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
} | ||
|
||
struct S2 { | ||
field: *mut u8, | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
} | ||
|
||
type Tuple1 = (*const u8, i32); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
type Tuple2 = (*mut u8, i32); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
type Array1 = [*const u8; 10]; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
type Array2 = [*mut u8; 10]; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
type Alias1 = *const u8; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
type Alias2 = *mut u8; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P9: *const u8 = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P10: *const u8 = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
impl S1 { | ||
fn method(self, size: *const u32) {} | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
} | ||
|
||
trait Trait1 { | ||
fn method(p: *const u8); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
} | ||
|
||
fn generic_func<T>() -> *const T { std::ptr::null() } | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
//@ run-rustfix | ||
|
||
#![allow(unused)] | ||
|
||
pub const P1: const* u8 = 0 as _; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P2: mut* u8 = 1 as _; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P3: const* i32 = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P4: const* i32 = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P5: mut* i32 = std::ptr::null_mut(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P6: mut* i32 = std::ptr::null_mut(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P7: const* Vec<u8> = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P8: const* std::collections::HashMap<String, i32> = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
fn func1(p: const* u8) {} | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
fn func2(p: mut* u8) {} | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
fn func3() -> const* u8 { std::ptr::null() } | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
fn func4() -> mut* u8 { std::ptr::null_mut() } | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
struct S1 { | ||
field: const* u8, | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
} | ||
|
||
struct S2 { | ||
field: mut* u8, | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
} | ||
|
||
type Tuple1 = (const* u8, i32); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
type Tuple2 = (mut* u8, i32); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
type Array1 = [const* u8; 10]; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
type Array2 = [mut* u8; 10]; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
type Alias1 = const* u8; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
type Alias2 = mut* u8; | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P9: const *u8 = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
pub const P10: const * u8 = std::ptr::null(); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
impl S1 { | ||
fn method(self, size: const* u32) {} | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
} | ||
|
||
trait Trait1 { | ||
fn method(p: const* u8); | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
} | ||
|
||
fn generic_func<T>() -> const* T { std::ptr::null() } | ||
//~^ ERROR: raw pointer type must have a star before the mutability keyword | ||
//~| HELP: move it to the other side | ||
|
||
fn main() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.