diff --git a/src/librustc_ast/util/comments.rs b/src/librustc_ast/util/comments.rs index 9874754fcd2f7..39921b2022606 100644 --- a/src/librustc_ast/util/comments.rs +++ b/src/librustc_ast/util/comments.rs @@ -2,7 +2,7 @@ pub use CommentStyle::*; use crate::ast; use rustc_span::source_map::SourceMap; -use rustc_span::{BytePos, CharPos, FileName, Pos}; +use rustc_span::{BytePos, CharPos, FileName, Pos, Symbol}; use log::debug; @@ -52,7 +52,8 @@ pub fn is_doc_comment(s: &str) -> bool { || s.starts_with("/*!") } -pub fn doc_comment_style(comment: &str) -> ast::AttrStyle { +pub fn doc_comment_style(comment: Symbol) -> ast::AttrStyle { + let comment = &comment.as_str(); assert!(is_doc_comment(comment)); if comment.starts_with("//!") || comment.starts_with("/*!") { ast::AttrStyle::Inner @@ -61,7 +62,9 @@ pub fn doc_comment_style(comment: &str) -> ast::AttrStyle { } } -pub fn strip_doc_comment_decoration(comment: &str) -> String { +pub fn strip_doc_comment_decoration(comment: Symbol) -> String { + let comment = &comment.as_str(); + /// remove whitespace-only lines from the start/end of lines fn vertical_trim(lines: Vec) -> Vec { let mut i = 0; diff --git a/src/librustc_ast/util/comments/tests.rs b/src/librustc_ast/util/comments/tests.rs index f9cd69fb50d74..f08011fe4f862 100644 --- a/src/librustc_ast/util/comments/tests.rs +++ b/src/librustc_ast/util/comments/tests.rs @@ -1,47 +1,58 @@ use super::*; +use crate::with_default_session_globals; #[test] fn test_block_doc_comment_1() { - let comment = "/**\n * Test \n ** Test\n * Test\n*/"; - let stripped = strip_doc_comment_decoration(comment); - assert_eq!(stripped, " Test \n* Test\n Test"); + with_default_session_globals(|| { + let comment = "/**\n * Test \n ** Test\n * Test\n*/"; + let stripped = strip_doc_comment_decoration(Symbol::intern(comment)); + assert_eq!(stripped, " Test \n* Test\n Test"); + }) } #[test] fn test_block_doc_comment_2() { - let comment = "/**\n * Test\n * Test\n*/"; - let stripped = strip_doc_comment_decoration(comment); - assert_eq!(stripped, " Test\n Test"); + with_default_session_globals(|| { + let comment = "/**\n * Test\n * Test\n*/"; + let stripped = strip_doc_comment_decoration(Symbol::intern(comment)); + assert_eq!(stripped, " Test\n Test"); + }) } #[test] fn test_block_doc_comment_3() { - let comment = "/**\n let a: *i32;\n *a = 5;\n*/"; - let stripped = strip_doc_comment_decoration(comment); - assert_eq!(stripped, " let a: *i32;\n *a = 5;"); + with_default_session_globals(|| { + let comment = "/**\n let a: *i32;\n *a = 5;\n*/"; + let stripped = strip_doc_comment_decoration(Symbol::intern(comment)); + assert_eq!(stripped, " let a: *i32;\n *a = 5;"); + }) } #[test] fn test_block_doc_comment_4() { - let comment = "/*******************\n test\n *********************/"; - let stripped = strip_doc_comment_decoration(comment); - assert_eq!(stripped, " test"); + with_default_session_globals(|| { + let comment = "/*******************\n test\n *********************/"; + let stripped = strip_doc_comment_decoration(Symbol::intern(comment)); + assert_eq!(stripped, " test"); + }) } #[test] fn test_line_doc_comment() { - let stripped = strip_doc_comment_decoration("/// test"); - assert_eq!(stripped, " test"); - let stripped = strip_doc_comment_decoration("///! test"); - assert_eq!(stripped, " test"); - let stripped = strip_doc_comment_decoration("// test"); - assert_eq!(stripped, " test"); - let stripped = strip_doc_comment_decoration("// test"); - assert_eq!(stripped, " test"); - let stripped = strip_doc_comment_decoration("///test"); - assert_eq!(stripped, "test"); - let stripped = strip_doc_comment_decoration("///!test"); - assert_eq!(stripped, "test"); - let stripped = strip_doc_comment_decoration("//test"); - assert_eq!(stripped, "test"); + with_default_session_globals(|| { + let stripped = strip_doc_comment_decoration(Symbol::intern("/// test")); + assert_eq!(stripped, " test"); + let stripped = strip_doc_comment_decoration(Symbol::intern("///! test")); + assert_eq!(stripped, " test"); + let stripped = strip_doc_comment_decoration(Symbol::intern("// test")); + assert_eq!(stripped, " test"); + let stripped = strip_doc_comment_decoration(Symbol::intern("// test")); + assert_eq!(stripped, " test"); + let stripped = strip_doc_comment_decoration(Symbol::intern("///test")); + assert_eq!(stripped, "test"); + let stripped = strip_doc_comment_decoration(Symbol::intern("///!test")); + assert_eq!(stripped, "test"); + let stripped = strip_doc_comment_decoration(Symbol::intern("//test")); + assert_eq!(stripped, "test"); + }) } diff --git a/src/librustc_ast/util/lev_distance.rs b/src/librustc_ast/util/lev_distance.rs index cce86fed9891c..d4e0e3ba051c9 100644 --- a/src/librustc_ast/util/lev_distance.rs +++ b/src/librustc_ast/util/lev_distance.rs @@ -47,12 +47,13 @@ pub fn lev_distance(a: &str, b: &str) -> usize { /// a lower(upper)case letters mismatch. pub fn find_best_match_for_name<'a, T>( iter_names: T, - lookup: &str, + lookup: Symbol, dist: Option, ) -> Option where T: Iterator, { + let lookup = &lookup.as_str(); let max_dist = dist.map_or_else(|| cmp::max(lookup.len(), 3) / 3, |d| d); let name_vec: Vec<&Symbol> = iter_names.collect(); diff --git a/src/librustc_ast/util/lev_distance/tests.rs b/src/librustc_ast/util/lev_distance/tests.rs index e9b6c9759b644..94d56a3d7b4ae 100644 --- a/src/librustc_ast/util/lev_distance/tests.rs +++ b/src/librustc_ast/util/lev_distance/tests.rs @@ -25,31 +25,34 @@ fn test_find_best_match_for_name() { with_default_session_globals(|| { let input = vec![Symbol::intern("aaab"), Symbol::intern("aaabc")]; assert_eq!( - find_best_match_for_name(input.iter(), "aaaa", None), + find_best_match_for_name(input.iter(), Symbol::intern("aaaa"), None), Some(Symbol::intern("aaab")) ); - assert_eq!(find_best_match_for_name(input.iter(), "1111111111", None), None); + assert_eq!( + find_best_match_for_name(input.iter(), Symbol::intern("1111111111"), None), + None + ); let input = vec![Symbol::intern("aAAA")]; assert_eq!( - find_best_match_for_name(input.iter(), "AAAA", None), + find_best_match_for_name(input.iter(), Symbol::intern("AAAA"), None), Some(Symbol::intern("aAAA")) ); let input = vec![Symbol::intern("AAAA")]; // Returns None because `lev_distance > max_dist / 3` - assert_eq!(find_best_match_for_name(input.iter(), "aaaa", None), None); + assert_eq!(find_best_match_for_name(input.iter(), Symbol::intern("aaaa"), None), None); let input = vec![Symbol::intern("AAAA")]; assert_eq!( - find_best_match_for_name(input.iter(), "aaaa", Some(4)), + find_best_match_for_name(input.iter(), Symbol::intern("aaaa"), Some(4)), Some(Symbol::intern("AAAA")) ); let input = vec![Symbol::intern("a_longer_variable_name")]; assert_eq!( - find_best_match_for_name(input.iter(), "a_variable_longer_name", None), + find_best_match_for_name(input.iter(), Symbol::intern("a_variable_longer_name"), None), Some(Symbol::intern("a_longer_variable_name")) ); }) diff --git a/src/librustc_ast_pretty/pprust.rs b/src/librustc_ast_pretty/pprust.rs index 2d803262f79e1..cf43e5bd3528a 100644 --- a/src/librustc_ast_pretty/pprust.rs +++ b/src/librustc_ast_pretty/pprust.rs @@ -522,6 +522,10 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere self.word(st) } + fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) { + self.print_string(&sym.as_str(), style); + } + fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) { self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true) } @@ -2050,7 +2054,7 @@ impl<'a> State<'a> { let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r { InlineAsmRegOrRegClass::Reg(r) => { - s.print_string(&r.as_str(), ast::StrStyle::Cooked) + s.print_symbol(*r, ast::StrStyle::Cooked) } InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()), }; @@ -2144,7 +2148,7 @@ impl<'a> State<'a> { ast::ExprKind::LlvmInlineAsm(ref a) => { self.s.word("llvm_asm!"); self.popen(); - self.print_string(&a.asm.as_str(), a.asm_str_style); + self.print_symbol(a.asm, a.asm_str_style); self.word_space(":"); self.commasep(Inconsistent, &a.outputs, |s, out| { @@ -2164,7 +2168,7 @@ impl<'a> State<'a> { self.word_space(":"); self.commasep(Inconsistent, &a.inputs, |s, &(co, ref o)| { - s.print_string(&co.as_str(), ast::StrStyle::Cooked); + s.print_symbol(co, ast::StrStyle::Cooked); s.popen(); s.print_expr(o); s.pclose(); @@ -2172,8 +2176,8 @@ impl<'a> State<'a> { self.s.space(); self.word_space(":"); - self.commasep(Inconsistent, &a.clobbers, |s, co| { - s.print_string(&co.as_str(), ast::StrStyle::Cooked); + self.commasep(Inconsistent, &a.clobbers, |s, &co| { + s.print_symbol(co, ast::StrStyle::Cooked); }); let mut options = vec![]; diff --git a/src/librustc_expand/proc_macro_server.rs b/src/librustc_expand/proc_macro_server.rs index ce53b9c0b66c8..2805b4203f928 100644 --- a/src/librustc_expand/proc_macro_server.rs +++ b/src/librustc_expand/proc_macro_server.rs @@ -149,8 +149,8 @@ impl FromInternal<(TreeAndJoint, &'_ ParseSess, &'_ mut Vec)> } Literal(lit) => tt!(Literal { lit }), DocComment(c) => { - let style = comments::doc_comment_style(&c.as_str()); - let stripped = comments::strip_doc_comment_decoration(&c.as_str()); + let style = comments::doc_comment_style(c); + let stripped = comments::strip_doc_comment_decoration(c); let mut escaped = String::new(); for ch in stripped.chars() { escaped.extend(ch.escape_debug()); diff --git a/src/librustc_hir_pretty/lib.rs b/src/librustc_hir_pretty/lib.rs index c16b7c63e3147..2298a80ae4f1f 100644 --- a/src/librustc_hir_pretty/lib.rs +++ b/src/librustc_hir_pretty/lib.rs @@ -1557,7 +1557,7 @@ impl<'a> State<'a> { let i = &a.inner; self.s.word("llvm_asm!"); self.popen(); - self.print_string(&i.asm.as_str(), i.asm_str_style); + self.print_symbol(i.asm, i.asm_str_style); self.word_space(":"); let mut out_idx = 0; @@ -1579,8 +1579,8 @@ impl<'a> State<'a> { self.word_space(":"); let mut in_idx = 0; - self.commasep(Inconsistent, &i.inputs, |s, co| { - s.print_string(&co.as_str(), ast::StrStyle::Cooked); + self.commasep(Inconsistent, &i.inputs, |s, &co| { + s.print_symbol(co, ast::StrStyle::Cooked); s.popen(); s.print_expr(&a.inputs_exprs[in_idx]); s.pclose(); @@ -1589,8 +1589,8 @@ impl<'a> State<'a> { self.s.space(); self.word_space(":"); - self.commasep(Inconsistent, &i.clobbers, |s, co| { - s.print_string(&co.as_str(), ast::StrStyle::Cooked); + self.commasep(Inconsistent, &i.clobbers, |s, &co| { + s.print_symbol(co, ast::StrStyle::Cooked); }); let mut options = vec![]; diff --git a/src/librustc_incremental/assert_module_sources.rs b/src/librustc_incremental/assert_module_sources.rs index 1a1dbc1b5a86f..cd5da7a67685c 100644 --- a/src/librustc_incremental/assert_module_sources.rs +++ b/src/librustc_incremental/assert_module_sources.rs @@ -139,7 +139,7 @@ impl AssertModuleSource<'tcx> { } self.tcx.sess.cgu_reuse_tracker.set_expectation( - &cgu_name.as_str(), + cgu_name, &user_path, attr.span, expected_reuse, diff --git a/src/librustc_incremental/persist/dirty_clean.rs b/src/librustc_incremental/persist/dirty_clean.rs index 043aff90ce404..ddc1def6e9367 100644 --- a/src/librustc_incremental/persist/dirty_clean.rs +++ b/src/librustc_incremental/persist/dirty_clean.rs @@ -234,7 +234,7 @@ impl DirtyCleanVisitor<'tcx> { for item in attr.meta_item_list().unwrap_or_else(Vec::new) { if item.check_name(LABEL) { let value = expect_associated_value(self.tcx, &item); - return Some(self.resolve_labels(&item, &value.as_str())); + return Some(self.resolve_labels(&item, value)); } } None @@ -245,7 +245,7 @@ impl DirtyCleanVisitor<'tcx> { for item in attr.meta_item_list().unwrap_or_else(Vec::new) { if item.check_name(EXCEPT) { let value = expect_associated_value(self.tcx, &item); - return self.resolve_labels(&item, &value.as_str()); + return self.resolve_labels(&item, value); } } // if no `label` or `except` is given, only the node's group are asserted @@ -347,9 +347,9 @@ impl DirtyCleanVisitor<'tcx> { (name, labels) } - fn resolve_labels(&self, item: &NestedMetaItem, value: &str) -> Labels { + fn resolve_labels(&self, item: &NestedMetaItem, value: Symbol) -> Labels { let mut out = Labels::default(); - for label in value.split(',') { + for label in value.as_str().split(',') { let label = label.trim(); if DepNode::has_label_string(label) { if out.contains(label) { diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index 8fb7d5fcae176..c83f1171735ef 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -427,11 +427,8 @@ pub(crate) fn check_attr_crate_type(attrs: &[ast::Attribute], lint_buffer: &mut if let ast::MetaItemKind::NameValue(spanned) = a.meta().unwrap().kind { let span = spanned.span; - let lev_candidate = find_best_match_for_name( - CRATE_TYPES.iter().map(|(k, _)| k), - &n.as_str(), - None, - ); + let lev_candidate = + find_best_match_for_name(CRATE_TYPES.iter().map(|(k, _)| k), n, None); if let Some(candidate) = lev_candidate { lint_buffer.buffer_lint_with_diagnostic( lint::builtin::UNKNOWN_CRATE_TYPES, diff --git a/src/librustc_lint/context.rs b/src/librustc_lint/context.rs index edbeea6db41cd..65fd938a11351 100644 --- a/src/librustc_lint/context.rs +++ b/src/librustc_lint/context.rs @@ -394,8 +394,11 @@ impl LintStore { let symbols = self.by_name.keys().map(|name| Symbol::intern(&name)).collect::>(); - let suggestion = - find_best_match_for_name(symbols.iter(), &lint_name.to_lowercase(), None); + let suggestion = find_best_match_for_name( + symbols.iter(), + Symbol::intern(&lint_name.to_lowercase()), + None, + ); CheckLintNameResult::NoLint(suggestion) } diff --git a/src/librustc_metadata/link_args.rs b/src/librustc_metadata/link_args.rs index 2dd4a9c9dbcb1..c5a43b91b5e9b 100644 --- a/src/librustc_metadata/link_args.rs +++ b/src/librustc_metadata/link_args.rs @@ -1,7 +1,7 @@ use rustc_hir as hir; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_middle::ty::TyCtxt; -use rustc_span::symbol::sym; +use rustc_span::symbol::{sym, Symbol}; use rustc_target::spec::abi::Abi; crate fn collect(tcx: TyCtxt<'_>) -> Vec { @@ -11,7 +11,7 @@ crate fn collect(tcx: TyCtxt<'_>) -> Vec { for attr in tcx.hir().krate().item.attrs.iter() { if attr.has_name(sym::link_args) { if let Some(linkarg) = attr.value_str() { - collector.add_link_args(&linkarg.as_str()); + collector.add_link_args(linkarg); } } } @@ -36,7 +36,7 @@ impl<'tcx> ItemLikeVisitor<'tcx> for Collector { // First, add all of the custom #[link_args] attributes for m in it.attrs.iter().filter(|a| a.check_name(sym::link_args)) { if let Some(linkarg) = m.value_str() { - self.add_link_args(&linkarg.as_str()); + self.add_link_args(linkarg); } } } @@ -46,7 +46,7 @@ impl<'tcx> ItemLikeVisitor<'tcx> for Collector { } impl Collector { - fn add_link_args(&mut self, args: &str) { - self.args.extend(args.split(' ').filter(|s| !s.is_empty()).map(|s| s.to_string())) + fn add_link_args(&mut self, args: Symbol) { + self.args.extend(args.as_str().split(' ').filter(|s| !s.is_empty()).map(|s| s.to_string())) } } diff --git a/src/librustc_middle/mir/mono.rs b/src/librustc_middle/mir/mono.rs index f1c1b962ab997..c9e5a196f9179 100644 --- a/src/librustc_middle/mir/mono.rs +++ b/src/librustc_middle/mir/mono.rs @@ -448,8 +448,7 @@ impl CodegenUnitNameBuilder<'tcx> { if self.tcx.sess.opts.debugging_opts.human_readable_cgu_names { cgu_name } else { - let cgu_name = &cgu_name.as_str(); - Symbol::intern(&CodegenUnit::mangle_name(cgu_name)) + Symbol::intern(&CodegenUnit::mangle_name(&cgu_name.as_str())) } } diff --git a/src/librustc_parse/parser/attr.rs b/src/librustc_parse/parser/attr.rs index 803f14a2a228a..8b67f4743c6b6 100644 --- a/src/librustc_parse/parser/attr.rs +++ b/src/librustc_parse/parser/attr.rs @@ -74,7 +74,7 @@ impl<'a> Parser<'a> { } fn mk_doc_comment(&self, s: Symbol) -> ast::Attribute { - attr::mk_doc_comment(comments::doc_comment_style(&s.as_str()), s, self.token.span) + attr::mk_doc_comment(comments::doc_comment_style(s), s, self.token.span) } /// Matches `attribute = # ! [ meta_item ]`. diff --git a/src/librustc_parse/parser/mod.rs b/src/librustc_parse/parser/mod.rs index 61c680469f03c..72866468b6560 100644 --- a/src/librustc_parse/parser/mod.rs +++ b/src/librustc_parse/parser/mod.rs @@ -213,7 +213,7 @@ impl TokenCursor { tok => return tok, }; - let stripped = strip_doc_comment_decoration(&name.as_str()); + let stripped = strip_doc_comment_decoration(name); // Searches for the occurrences of `"#*` and returns the minimum number of `#`s // required to wrap the text. @@ -250,7 +250,7 @@ impl TokenCursor { TokenCursorFrame::new( delim_span, token::NoDelim, - &if doc_comment_style(&name.as_str()) == AttrStyle::Inner { + &if doc_comment_style(name) == AttrStyle::Inner { [TokenTree::token(token::Pound, sp), TokenTree::token(token::Not, sp), body] .iter() .cloned() diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index b98e4f0b3a27b..4f25b948eb66e 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -674,7 +674,7 @@ impl<'a> Resolver<'a> { match find_best_match_for_name( suggestions.iter().map(|suggestion| &suggestion.candidate), - &ident.as_str(), + ident.name, None, ) { Some(found) if found != ident.name => { diff --git a/src/librustc_resolve/imports.rs b/src/librustc_resolve/imports.rs index f909f66b70746..d3f45f962a025 100644 --- a/src/librustc_resolve/imports.rs +++ b/src/librustc_resolve/imports.rs @@ -1132,7 +1132,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { }); let lev_suggestion = - find_best_match_for_name(names, &ident.as_str(), None).map(|suggestion| { + find_best_match_for_name(names, ident.name, None).map(|suggestion| { ( vec![(ident.span, suggestion.to_string())], String::from("a similar name exists in the module"), diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 679f5637686ff..fb4c4025123a0 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -760,7 +760,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { self.r.report_error( original_span, ResolutionError::UnreachableLabel { - name: &label.name.as_str(), + name: label.name, definition_span: ident.span, suggestion, }, @@ -777,7 +777,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { self.r.report_error( original_span, - ResolutionError::UndeclaredLabel { name: &label.name.as_str(), suggestion }, + ResolutionError::UndeclaredLabel { name: label.name, suggestion }, ); None } @@ -1550,7 +1550,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { // `Variant(a, a)`: _ => IdentifierBoundMoreThanOnceInSamePattern, }; - self.r.report_error(ident.span, error(&ident.as_str())); + self.r.report_error(ident.span, error(ident.name)); } // Record as bound if it's valid: diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index fc41ce5d53511..a08e46ef99843 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -765,7 +765,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> { match find_best_match_for_name( names.iter().map(|suggestion| &suggestion.candidate), - &name.as_str(), + name, None, ) { Some(found) if found != name => { @@ -1008,7 +1008,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> { .filter(|(id, _)| id.span.ctxt() == label.span.ctxt()) .map(|(id, _)| &id.name); - find_best_match_for_name(names, &label.as_str(), None).map(|symbol| { + find_best_match_for_name(names, label.name, None).map(|symbol| { // Upon finding a similar name, get the ident that it was from - the span // contained within helps make a useful diagnostic. In addition, determine // whether this candidate is within scope. diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 0f1618031d034..a265c15c18bc9 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -193,11 +193,11 @@ enum ResolutionError<'a> { /// Error E0409: variable `{}` is bound in inconsistent ways within the same match arm. VariableBoundWithDifferentMode(Symbol, Span), /// Error E0415: identifier is bound more than once in this parameter list. - IdentifierBoundMoreThanOnceInParameterList(&'a str), + IdentifierBoundMoreThanOnceInParameterList(Symbol), /// Error E0416: identifier is bound more than once in the same pattern. - IdentifierBoundMoreThanOnceInSamePattern(&'a str), + IdentifierBoundMoreThanOnceInSamePattern(Symbol), /// Error E0426: use of undeclared label. - UndeclaredLabel { name: &'a str, suggestion: Option }, + UndeclaredLabel { name: Symbol, suggestion: Option }, /// Error E0429: `self` imports are only allowed within a `{ }` list. SelfImportsOnlyAllowedWithin { root: bool, span_with_rename: Span }, /// Error E0430: `self` import can only appear once in the list. @@ -211,13 +211,13 @@ enum ResolutionError<'a> { /// Error E0435: attempt to use a non-constant value in a constant. AttemptToUseNonConstantValueInConstant, /// Error E0530: `X` bindings cannot shadow `Y`s. - BindingShadowsSomethingUnacceptable(&'a str, Symbol, &'a NameBinding<'a>), + BindingShadowsSomethingUnacceptable(&'static str, Symbol, &'a NameBinding<'a>), /// Error E0128: type parameters with a default cannot use forward-declared identifiers. ForwardDeclaredTyParam, // FIXME(const_generics:defaults) /// Error E0735: type parameters with a default cannot use `Self` SelfInTyParamDefault, /// Error E0767: use of unreachable label - UnreachableLabel { name: &'a str, definition_span: Span, suggestion: Option }, + UnreachableLabel { name: Symbol, definition_span: Span, suggestion: Option }, } enum VisResolutionError<'a> { diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index 5ecb256719f1f..e29bc9f078ddb 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -824,7 +824,7 @@ impl<'tcx> SaveContext<'tcx> { for attr in attrs { if let Some(val) = attr.doc_str() { if attr.is_doc_comment() { - result.push_str(&strip_doc_comment_decoration(&val.as_str())); + result.push_str(&strip_doc_comment_decoration(val)); } else { result.push_str(&val.as_str()); } diff --git a/src/librustc_session/cgu_reuse_tracker.rs b/src/librustc_session/cgu_reuse_tracker.rs index 2f06ff95b1923..ace233611223c 100644 --- a/src/librustc_session/cgu_reuse_tracker.rs +++ b/src/librustc_session/cgu_reuse_tracker.rs @@ -4,7 +4,7 @@ use log::debug; use rustc_data_structures::fx::FxHashMap; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; use std::sync::{Arc, Mutex}; #[derive(Copy, Clone, Debug, PartialEq, PartialOrd)] @@ -67,7 +67,7 @@ impl CguReuseTracker { pub fn set_expectation( &self, - cgu_name: &str, + cgu_name: Symbol, cgu_user_name: &str, error_span: Span, expected_reuse: CguReuse, diff --git a/src/librustc_span/symbol.rs b/src/librustc_span/symbol.rs index 95f36d3346ad7..0e9df5feb32ba 100644 --- a/src/librustc_span/symbol.rs +++ b/src/librustc_span/symbol.rs @@ -1597,7 +1597,7 @@ impl !Sync for SymbolStr {} /// This impl means that if `ss` is a `SymbolStr`: /// - `*ss` is a `str`; -/// - `&*ss` is a `&str`; +/// - `&*ss` is a `&str` (and `match &*ss { ... }` is a common pattern). /// - `&ss as &str` is a `&str`, which means that `&ss` can be passed to a /// function expecting a `&str`. impl std::ops::Deref for SymbolStr { diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 128e88bb10ac5..616f5d90395e1 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -2254,7 +2254,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { .collect(); if let (Some(suggested_name), true) = ( - find_best_match_for_name(all_candidate_names.iter(), &assoc_name.as_str(), None), + find_best_match_for_name(all_candidate_names.iter(), assoc_name.name, None), assoc_name.span != DUMMY_SP, ) { err.span_suggestion( @@ -2354,7 +2354,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { let adt_def = qself_ty.ty_adt_def().expect("enum is not an ADT"); if let Some(suggested_name) = find_best_match_for_name( adt_def.variants.iter().map(|variant| &variant.ident.name), - &assoc_ident.as_str(), + assoc_ident.name, None, ) { err.span_suggestion( diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index e6b51f4c2cd2a..508f0cf1a9115 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1336,7 +1336,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // prevent all specified fields from being suggested let skip_fields = skip_fields.iter().map(|ref x| x.ident.name); if let Some(field_name) = - Self::suggest_field_name(variant, &field.ident.as_str(), skip_fields.collect()) + Self::suggest_field_name(variant, field.ident.name, skip_fields.collect()) { err.span_suggestion( field.ident.span, @@ -1377,7 +1377,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Return an hint about the closest match in field names fn suggest_field_name( variant: &'tcx ty::VariantDef, - field: &str, + field: Symbol, skip: Vec, ) -> Option { let names = variant.fields.iter().filter_map(|field| { @@ -1621,7 +1621,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { field: Ident, ) { if let Some(suggested_field_name) = - Self::suggest_field_name(def.non_enum_variant(), &field.as_str(), vec![]) + Self::suggest_field_name(def.non_enum_variant(), field.name, vec![]) { err.span_suggestion( field.span, diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index ba952df7e4e28..9c5e3cbc93844 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -1536,7 +1536,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } else { let best_name = { let names = applicable_close_candidates.iter().map(|cand| &cand.ident.name); - find_best_match_for_name(names, &self.method_name.unwrap().as_str(), None) + find_best_match_for_name(names, self.method_name.unwrap().name, None) } .unwrap(); Ok(applicable_close_candidates diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 611406bd052dc..b8e26fc487140 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -729,7 +729,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let adt_def = actual.ty_adt_def().expect("enum is not an ADT"); if let Some(suggestion) = lev_distance::find_best_match_for_name( adt_def.variants.iter().map(|s| &s.ident.name), - &item_name.as_str(), + item_name.name, None, ) { err.span_suggestion( diff --git a/src/librustc_typeck/check/pat.rs b/src/librustc_typeck/check/pat.rs index a654fc3dfc2df..18fc37df88652 100644 --- a/src/librustc_typeck/check/pat.rs +++ b/src/librustc_typeck/check/pat.rs @@ -1216,7 +1216,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); if plural == "" { let input = unmentioned_fields.iter().map(|field| &field.name); - let suggested_name = find_best_match_for_name(input, &ident.as_str(), None); + let suggested_name = find_best_match_for_name(input, ident.name, None); if let Some(suggested_name) = suggested_name { err.span_suggestion( ident.span, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 10a542eb25466..c9ae67ded0a3f 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -540,7 +540,7 @@ impl Attributes { .filter_map(|attr| { if let Some(value) = attr.doc_str() { let (value, mk_fragment): (_, fn(_, _, _) -> _) = if attr.is_doc_comment() { - (strip_doc_comment_decoration(&value.as_str()), DocFragment::SugaredDoc) + (strip_doc_comment_decoration(value), DocFragment::SugaredDoc) } else { (value.to_string(), DocFragment::RawDoc) }; diff --git a/src/tools/clippy/clippy_lints/src/attrs.rs b/src/tools/clippy/clippy_lints/src/attrs.rs index ef01364b7d965..c29432bf93384 100644 --- a/src/tools/clippy/clippy_lints/src/attrs.rs +++ b/src/tools/clippy/clippy_lints/src/attrs.rs @@ -421,7 +421,11 @@ fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMet .iter() .map(|l| Symbol::intern(&l.name_lower())) .collect::>(); - let sugg = find_best_match_for_name(symbols.iter(), &format!("clippy::{}", name_lower), None); + let sugg = find_best_match_for_name( + symbols.iter(), + Symbol::intern(&format!("clippy::{}", name_lower)), + None, + ); if lint_name.chars().any(char::is_uppercase) && lint_store.find_lints(&format!("clippy::{}", name_lower)).is_ok() {