mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-24 21:05:12 +00:00
Avoid suggesting self in visibility spec
Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>
This commit is contained in:
parent
b9a37ad0d9
commit
f3a08fd8e7
@ -298,11 +298,16 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
|||||||
.get(0)
|
.get(0)
|
||||||
.map(|p| (p.span.shrink_to_lo(), "&self, "))
|
.map(|p| (p.span.shrink_to_lo(), "&self, "))
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
|
// Try to look for the "(" after the function name, if possible.
|
||||||
|
// This avoids placing the suggestion into the visibility specifier.
|
||||||
|
let span = fn_kind
|
||||||
|
.ident()
|
||||||
|
.map_or(*span, |ident| span.with_lo(ident.span.hi()));
|
||||||
(
|
(
|
||||||
self.r
|
self.r
|
||||||
.session
|
.session
|
||||||
.source_map()
|
.source_map()
|
||||||
.span_through_char(*span, '(')
|
.span_through_char(span, '(')
|
||||||
.shrink_to_hi(),
|
.shrink_to_hi(),
|
||||||
"&self",
|
"&self",
|
||||||
)
|
)
|
||||||
|
15
src/test/ui/suggestions/suggest-add-self.rs
Normal file
15
src/test/ui/suggestions/suggest-add-self.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
struct X(i32);
|
||||||
|
|
||||||
|
impl X {
|
||||||
|
pub(crate) fn f() {
|
||||||
|
self.0
|
||||||
|
//~^ ERROR expected value, found module `self`
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn g() {
|
||||||
|
self.0
|
||||||
|
//~^ ERROR expected value, found module `self`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
29
src/test/ui/suggestions/suggest-add-self.stderr
Normal file
29
src/test/ui/suggestions/suggest-add-self.stderr
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
error[E0424]: expected value, found module `self`
|
||||||
|
--> $DIR/suggest-add-self.rs:5:9
|
||||||
|
|
|
||||||
|
LL | pub(crate) fn f() {
|
||||||
|
| - this function doesn't have a `self` parameter
|
||||||
|
LL | self.0
|
||||||
|
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
|
||||||
|
|
|
||||||
|
help: add a `self` receiver parameter to make the associated `fn` a method
|
||||||
|
|
|
||||||
|
LL | pub(crate) fn f(&self) {
|
||||||
|
| +++++
|
||||||
|
|
||||||
|
error[E0424]: expected value, found module `self`
|
||||||
|
--> $DIR/suggest-add-self.rs:10:9
|
||||||
|
|
|
||||||
|
LL | pub fn g() {
|
||||||
|
| - this function doesn't have a `self` parameter
|
||||||
|
LL | self.0
|
||||||
|
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
|
||||||
|
|
|
||||||
|
help: add a `self` receiver parameter to make the associated `fn` a method
|
||||||
|
|
|
||||||
|
LL | pub fn g(&self) {
|
||||||
|
| +++++
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0424`.
|
Loading…
Reference in New Issue
Block a user