From 99edcd4da77855d5ad2d439293e0789715f32ca0 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sun, 30 Sep 2018 20:47:15 +0800 Subject: [PATCH] lint to use self for this/my --- src/librustc_resolve/lib.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index a68c89deea5..99ce351cbcf 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -2981,13 +2981,13 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> { // Make the base error. let expected = source.descr_expected(); let path_str = names_to_string(path); + let item_str = path[path.len() - 1]; let code = source.error_code(def.is_some()); let (base_msg, fallback_label, base_span) = if let Some(def) = def { (format!("expected {}, found {} `{}`", expected, def.kind_name(), path_str), format!("not a {}", expected), span) } else { - let item_str = path[path.len() - 1]; let item_span = path[path.len() - 1].span; let (mod_prefix, mod_str) = if path.len() == 1 { (String::new(), "this scope".to_string()) @@ -3010,6 +3010,20 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> { let code = DiagnosticId::Error(code.into()); let mut err = this.session.struct_span_err_with_code(base_span, &base_msg, code); + // Emit help message for fake-self from other languages like `this`(javascript) + let fake_self: Vec = ["this", "my"].iter().map( + |s| Ident::from_str(*s) + ).collect(); + if fake_self.contains(&item_str) + && this.self_value_is_available(path[0].span, span) { + err.span_suggestion_with_applicability( + span, + "did you mean", + "self".to_string(), + Applicability::MachineApplicable, + ); + } + // Emit special messages for unresolved `Self` and `self`. if is_self_type(path, ns) { __diagnostic_used!(E0411);