Skip to content

Commit

Permalink
Prefer Symbol to Ident when there's no sensible Span
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Sep 15, 2019
1 parent ca3766e commit e8d2f62
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
17 changes: 6 additions & 11 deletions src/librustc_lint/unused.rs
Expand Up @@ -618,24 +618,19 @@ impl UnusedImportBraces {
}

// Trigger the lint if the nested item is a non-self single item
let node_ident;
match items[0].0.kind {
let node_name = match items[0].0.kind {
ast::UseTreeKind::Simple(rename, ..) => {
let orig_ident = items[0].0.prefix.segments.last().unwrap().ident;
if orig_ident.name == kw::SelfLower {
return;
}
node_ident = rename.unwrap_or(orig_ident);
rename.unwrap_or(orig_ident).name
}
ast::UseTreeKind::Glob => {
node_ident = ast::Ident::from_str("*");
}
ast::UseTreeKind::Nested(_) => {
return;
}
}
ast::UseTreeKind::Glob => Symbol::intern("*"),
ast::UseTreeKind::Nested(_) => return,
};

let msg = format!("braces around {} is unnecessary", node_ident.name);
let msg = format!("braces around {} is unnecessary", node_name);
cx.span_lint(UNUSED_IMPORT_BRACES, item.span, &msg);
}
}
Expand Down
34 changes: 16 additions & 18 deletions src/librustc_resolve/lib.rs
Expand Up @@ -40,7 +40,7 @@ use rustc_metadata::cstore::CStore;
use syntax::ext::hygiene::{ExpnId, Transparency, SyntaxContext};
use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy};
use syntax::ext::base::{SyntaxExtension, MacroKind, SpecialDerives};
use syntax::symbol::{Symbol, kw, sym};
use syntax::symbol::{kw, sym};

use syntax::visit::{self, Visitor};
use syntax::attr;
Expand Down Expand Up @@ -241,7 +241,7 @@ impl Segment {

fn names_to_string(segments: &[Segment]) -> String {
names_to_string(&segments.iter()
.map(|seg| seg.ident)
.map(|seg| seg.ident.name)
.collect::<Vec<_>>())
}
}
Expand Down Expand Up @@ -951,7 +951,7 @@ pub struct Resolver<'a> {
struct_constructors: DefIdMap<(Res, ty::Visibility)>,

/// Features enabled for this crate.
active_features: FxHashSet<Symbol>,
active_features: FxHashSet<Name>,

/// Stores enum visibilities to properly build a reduced graph
/// when visiting the correspondent variants.
Expand Down Expand Up @@ -1018,8 +1018,8 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
fn resolve_str_path(
&mut self,
span: Span,
crate_root: Option<Symbol>,
components: &[Symbol],
crate_root: Option<Name>,
components: &[Name],
ns: Namespace,
) -> (ast::Path, Res) {
let root = if crate_root.is_some() {
Expand Down Expand Up @@ -2555,7 +2555,7 @@ impl<'a> Resolver<'a> {
fn add_suggestion_for_rename_of_use(
&self,
err: &mut DiagnosticBuilder<'_>,
name: Symbol,
name: Name,
directive: &ImportDirective<'_>,
binding_span: Span,
) {
Expand Down Expand Up @@ -2770,38 +2770,37 @@ impl<'a> Resolver<'a> {
}
}

fn names_to_string(idents: &[Ident]) -> String {
fn names_to_string(names: &[Name]) -> String {
let mut result = String::new();
for (i, ident) in idents.iter()
.filter(|ident| ident.name != kw::PathRoot)
for (i, name) in names.iter()
.filter(|name| **name != kw::PathRoot)
.enumerate() {
if i > 0 {
result.push_str("::");
}
result.push_str(&ident.as_str());
result.push_str(&name.as_str());
}
result
}

fn path_names_to_string(path: &Path) -> String {
names_to_string(&path.segments.iter()
.map(|seg| seg.ident)
.map(|seg| seg.ident.name)
.collect::<Vec<_>>())
}

/// A somewhat inefficient routine to obtain the name of a module.
fn module_to_string(module: Module<'_>) -> Option<String> {
let mut names = Vec::new();

fn collect_mod(names: &mut Vec<Ident>, module: Module<'_>) {
fn collect_mod(names: &mut Vec<Name>, module: Module<'_>) {
if let ModuleKind::Def(.., name) = module.kind {
if let Some(parent) = module.parent {
names.push(Ident::with_dummy_span(name));
names.push(name);
collect_mod(names, parent);
}
} else {
// danger, shouldn't be ident?
names.push(Ident::from_str("<opaque>"));
names.push(Name::intern("<opaque>"));
collect_mod(names, module.parent.unwrap());
}
}
Expand All @@ -2810,9 +2809,8 @@ fn module_to_string(module: Module<'_>) -> Option<String> {
if names.is_empty() {
return None;
}
Some(names_to_string(&names.into_iter()
.rev()
.collect::<Vec<_>>()))
names.reverse();
Some(names_to_string(&names))
}

#[derive(Copy, Clone, Debug)]
Expand Down
10 changes: 6 additions & 4 deletions src/librustc_resolve/resolve_imports.rs
Expand Up @@ -1433,15 +1433,17 @@ fn import_path_to_string(names: &[Ident],
let global = !names.is_empty() && names[0].name == kw::PathRoot;
if let Some(pos) = pos {
let names = if global { &names[1..pos + 1] } else { &names[..pos + 1] };
names_to_string(names)
names_to_string(&names.iter().map(|ident| ident.name).collect::<Vec<_>>())
} else {
let names = if global { &names[1..] } else { names };
if names.is_empty() {
import_directive_subclass_to_string(subclass)
} else {
format!("{}::{}",
names_to_string(names),
import_directive_subclass_to_string(subclass))
format!(
"{}::{}",
names_to_string(&names.iter().map(|ident| ident.name).collect::<Vec<_>>()),
import_directive_subclass_to_string(subclass),
)
}
}
}
Expand Down

0 comments on commit e8d2f62

Please sign in to comment.