1
Fork 0

Format all the let chains in compiler

This commit is contained in:
Michael Goulet 2023-10-13 08:58:33 +00:00
parent 2763ca50da
commit b2d2184ede
206 changed files with 3120 additions and 2228 deletions

View file

@ -187,7 +187,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} else if let Some((span, msg, sugg, appl)) = suggestion {
err.span_suggestion_verbose(span, msg, sugg, appl);
err.emit();
} else if let [segment] = path.as_slice() && is_call {
} else if let [segment] = path.as_slice()
&& is_call
{
err.stash(segment.ident.span, rustc_errors::StashKey::CallIntoMethod);
} else {
err.emit();
@ -1690,7 +1692,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
non_exhaustive = Some(attr.span);
} else if let Some(span) = ctor_fields_span {
err.span_label(span, "a constructor is private if any of the fields is private");
if let Res::Def(_, d) = res && let Some(fields) = self.field_visibility_spans.get(&d) {
if let Res::Def(_, d) = res
&& let Some(fields) = self.field_visibility_spans.get(&d)
{
err.multipart_suggestion_verbose(
format!(
"consider making the field{} publicly accessible",
@ -1739,7 +1743,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
// Final step in the import chain, point out if the ADT is `non_exhaustive`
// which is probably why this privacy violation occurred.
if next_binding.is_none() && let Some(span) = non_exhaustive {
if next_binding.is_none()
&& let Some(span) = non_exhaustive
{
note_span.push_span_label(
span,
"cannot be constructed because it is `#[non_exhaustive]`",
@ -1846,7 +1852,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
parent_scope,
None,
ignore_binding,
).ok()
)
.ok()
} else if let Some(ribs) = ribs
&& let Some(TypeNS | ValueNS) = opt_ns
{
@ -1870,7 +1877,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
None,
false,
ignore_binding,
).ok()
)
.ok()
};
if let Some(binding) = binding {
let mut found = |what| {
@ -2232,7 +2240,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
// Add the import to the start, with a `{` if required.
let start_point = source_map.start_point(after_crate_name);
if is_definitely_crate && let Ok(start_snippet) = source_map.span_to_snippet(start_point) {
if is_definitely_crate
&& let Ok(start_snippet) = source_map.span_to_snippet(start_point)
{
corrections.push((
start_point,
if has_nested {
@ -2247,7 +2257,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
// Add a `};` to the end if nested, matching the `{` added at the start.
if !has_nested {
corrections.push((source_map.end_point(after_crate_name), "};".to_string()));
corrections
.push((source_map.end_point(after_crate_name), "};".to_string()));
}
} else {
// If the root import is module-relative, add the import separately
@ -2586,7 +2597,13 @@ fn show_candidates(
for candidate in &mut accessible_path_strings {
// produce an additional newline to separate the new use statement
// from the directly following item.
let additional_newline = if let FoundUse::No = found_use && let DiagnosticMode::Normal = mode { "\n" } else { "" };
let additional_newline = if let FoundUse::No = found_use
&& let DiagnosticMode::Normal = mode
{
"\n"
} else {
""
};
candidate.0 =
format!("{add_use}{}{append}{trailing}{additional_newline}", &candidate.0);
}

View file

@ -147,7 +147,8 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
warn_ambiguity |= nested_binding.warn_ambiguity;
}
if !is_ambiguity(binding, warn_ambiguity)
&& let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
&& let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local())
{
self.update_def(def_id, binding.vis.expect_local(), parent_id);
}
}

View file

@ -896,7 +896,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT {
if let NameBindingKind::Import { import, .. } = binding.kind
&& matches!(import.kind, ImportKind::MacroExport) {
&& matches!(import.kind, ImportKind::MacroExport)
{
self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
}
}
@ -928,9 +929,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if !self.is_accessible_from(import_vis, parent_scope.module) {
continue;
}
if let Some(ignored) = ignore_binding &&
let NameBindingKind::Import { import, .. } = ignored.kind &&
import == *single_import {
if let Some(ignored) = ignore_binding
&& let NameBindingKind::Import { import, .. } = ignored.kind
&& import == *single_import
{
// Ignore not just the binding itself, but if it has a shadowed_glob,
// ignore that, too, because this loop is supposed to only process
// named imports.
@ -1440,7 +1442,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
finalize,
ignore_binding,
)
} else if let Some(ribs) = ribs && let Some(TypeNS | ValueNS) = opt_ns {
} else if let Some(ribs) = ribs
&& let Some(TypeNS | ValueNS) = opt_ns
{
match self.resolve_ident_in_lexical_scope(
ident,
ns,

View file

@ -313,26 +313,20 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
(true, true) => {
// FIXME: remove `!binding.is_ambiguity()` after delete the warning ambiguity.
if !binding.is_ambiguity()
&& let NameBindingKind::Import { import: old_import, .. } = old_binding.kind
&& let NameBindingKind::Import { import: old_import, .. } =
old_binding.kind
&& let NameBindingKind::Import { import, .. } = binding.kind
&& old_import == import {
&& old_import == import
{
// We should replace the `old_binding` with `binding` regardless
// of whether they has same resolution or not when they are
// imported from the same glob-import statement.
resolution.binding = Some(binding);
} else if res != old_binding.res() {
let binding = if warn_ambiguity {
this.warn_ambiguity(
AmbiguityKind::GlobVsGlob,
old_binding,
binding,
)
this.warn_ambiguity(AmbiguityKind::GlobVsGlob, old_binding, binding)
} else {
this.ambiguity(
AmbiguityKind::GlobVsGlob,
old_binding,
binding,
)
this.ambiguity(AmbiguityKind::GlobVsGlob, old_binding, binding)
};
resolution.binding = Some(binding);
} else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
@ -434,7 +428,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let t = f(self, resolution);
if let Some(binding) = resolution.binding() && old_binding != Some(binding) {
if let Some(binding) = resolution.binding()
&& old_binding != Some(binding)
{
(binding, t, warn_ambiguity || old_binding.is_some())
} else {
return t;
@ -637,7 +633,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if binding.res() != Res::Err
&& glob_binding.res() != Res::Err
&& let NameBindingKind::Import { import: glob_import, .. } = glob_binding.kind
&& let NameBindingKind::Import { import: glob_import, .. } =
glob_binding.kind
&& let Some(binding_id) = binding_id
&& let Some(glob_import_id) = glob_import.id()
&& let glob_import_def_id = self.local_def_id(glob_import_id)
@ -738,11 +735,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
match &import.kind {
ImportKind::Single { source, .. } => {
if let Some(ModuleOrUniformRoot::Module(module)) = import.imported_module.get()
&& let Some(module) = module.opt_def_id()
&& let Some(module) = module.opt_def_id()
{
self.find_cfg_stripped(&mut diag, &source.name, module)
}
},
}
_ => {}
}
}
@ -989,10 +986,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
}
if !is_prelude
&& let Some(max_vis) = max_vis.get()
&& !max_vis.is_at_least(import.expect_vis(), self.tcx)
&& let Some(max_vis) = max_vis.get()
&& !max_vis.is_at_least(import.expect_vis(), self.tcx)
{
self.lint_buffer.buffer_lint(UNUSED_IMPORTS, id, import.span, fluent::resolve_glob_import_doesnt_reexport);
self.lint_buffer.buffer_lint(
UNUSED_IMPORTS,
id,
import.span,
fluent::resolve_glob_import_doesnt_reexport,
);
}
return None;
}

View file

@ -736,7 +736,8 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
// Check whether we should interpret this as a bare trait object.
if qself.is_none()
&& let Some(partial_res) = self.r.partial_res_map.get(&ty.id)
&& let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = partial_res.full_res()
&& let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) =
partial_res.full_res()
{
// This path is actually a bare trait object. In case of a bare `Fn`-trait
// object with anonymous lifetimes, we need this rib to correctly place the
@ -2046,7 +2047,9 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
if lifetime_count != 0 {
parameter_info.push(ElisionFnParameter {
index,
ident: if let Some(pat) = pat && let PatKind::Ident(_, ident, _) = pat.kind {
ident: if let Some(pat) = pat
&& let PatKind::Ident(_, ident, _) = pat.kind
{
Some(ident)
} else {
None
@ -2140,7 +2143,9 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
impl<'a> Visitor<'a> for SelfVisitor<'_, '_, '_> {
fn visit_ty(&mut self, ty: &'a Ty) {
trace!("SelfVisitor considering ty={:?}", ty);
if let TyKind::Ref(lt, ref mt) = ty.kind && self.is_self_ty(&mt.ty) {
if let TyKind::Ref(lt, ref mt) = ty.kind
&& self.is_self_ty(&mt.ty)
{
let lt_id = if let Some(lt) = lt {
lt.id
} else {
@ -3602,7 +3607,9 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
sugg.to_string(),
Applicability::MaybeIncorrect,
))
} else if res.is_none() && let PathSource::Type | PathSource::Expr(_) = source {
} else if res.is_none()
&& let PathSource::Type | PathSource::Expr(_) = source
{
this.suggest_adding_generic_parameter(path, source)
} else {
None
@ -4004,7 +4011,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
"unnecessary qualification",
lint::BuiltinLintDiagnostics::UnusedQualifications {
removal_span: finalize.path_span.until(last_segment.ident.span),
}
},
)
}
}
@ -4057,13 +4064,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
(block.could_be_bare_literal, &block.stmts[..])
&& let ExprKind::Type(..) = expr.kind
{
self.diagnostic_metadata.current_block_could_be_bare_struct_literal =
Some(block.span);
self.diagnostic_metadata.current_block_could_be_bare_struct_literal = Some(block.span);
}
// Descend into the block.
for stmt in &block.stmts {
if let StmtKind::Item(ref item) = stmt.kind
&& let ItemKind::MacroDef(..) = item.kind {
&& let ItemKind::MacroDef(..) = item.kind
{
num_macro_definition_ribs += 1;
let res = self.r.local_def_id(item.id).to_def_id();
self.ribs[ValueNS].push(Rib::new(RibKind::MacroDefinition(res)));
@ -4419,7 +4426,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
&& let Some(def_id) = res.opt_def_id()
&& !def_id.is_local()
&& self.r.tcx.crate_types().contains(&CrateType::ProcMacro)
&& matches!(self.r.tcx.sess.opts.resolve_doc_links, ResolveDocLinks::ExportedMetadata) {
&& matches!(
self.r.tcx.sess.opts.resolve_doc_links,
ResolveDocLinks::ExportedMetadata
)
{
// Encoding foreign def ids in proc macro crate metadata will ICE.
return None;
}

View file

@ -229,8 +229,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
i.ident.name == item_str.name
// Don't suggest if the item is in Fn signature arguments (#112590).
&& !sig.span.contains(item_span)
})
{
}) {
let sp = item_span.shrink_to_lo();
// Account for `Foo { field }` when suggesting `self.field` so we result on
@ -241,7 +240,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
}
_ => None,
};
let pre = if let Some(field) = field && field.is_shorthand {
let pre = if let Some(field) = field
&& field.is_shorthand
{
format!("{item_ident}: ")
} else {
String::new()
@ -254,13 +255,14 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
}
_ => matches!(
source,
PathSource::Expr(Some(Expr { kind: ExprKind::Call(..), ..})),
PathSource::Expr(Some(Expr { kind: ExprKind::Call(..), .. })),
),
};
match &item.kind {
AssocItemKind::Fn(fn_)
if (!sig.decl.has_self() || !is_call) && fn_.sig.decl.has_self() => {
if (!sig.decl.has_self() || !is_call) && fn_.sig.decl.has_self() =>
{
// Ensure that we only suggest `self.` if `self` is available,
// you can't call `fn foo(&self)` from `fn bar()` (#115992).
// We also want to mention that the method exists.
@ -270,19 +272,16 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
));
None
}
AssocItemKind::Fn(fn_)
if !fn_.sig.decl.has_self() && !is_call => {
AssocItemKind::Fn(fn_) if !fn_.sig.decl.has_self() && !is_call => {
span_label = Some((
item.ident.span,
"an associated function by that name is available on `Self` here",
));
None
}
AssocItemKind::Fn(fn_) if fn_.sig.decl.has_self() => Some((
sp,
"consider using the method on `Self`",
format!("{pre}self."),
)),
AssocItemKind::Fn(fn_) if fn_.sig.decl.has_self() => {
Some((sp, "consider using the method on `Self`", format!("{pre}self.")))
}
AssocItemKind::Fn(_) => Some((
sp,
"consider using the associated function on `Self`",
@ -293,7 +292,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
"consider using the associated constant on `Self`",
format!("{pre}Self::"),
)),
_ => None
_ => None,
}
} else {
None
@ -379,8 +378,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
prefix_path: &[Segment],
following_seg: Option<&Segment>,
) -> Vec<ImportSuggestion> {
if let Some(segment) = prefix_path.last() &&
let Some(following_seg) = following_seg
if let Some(segment) = prefix_path.last()
&& let Some(following_seg) = following_seg
{
let candidates = self.r.lookup_import_candidates(
segment.ident,
@ -392,12 +391,16 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
candidates
.into_iter()
.filter(|candidate| {
if let Some(def_id) = candidate.did &&
let Some(module) = self.r.get_module(def_id) {
Some(def_id) != self.parent_scope.module.opt_def_id() &&
self.r.resolutions(module).borrow().iter().any(|(key, _r)| {
key.ident.name == following_seg.ident.name
})
if let Some(def_id) = candidate.did
&& let Some(module) = self.r.get_module(def_id)
{
Some(def_id) != self.parent_scope.module.opt_def_id()
&& self
.r
.resolutions(module)
.borrow()
.iter()
.any(|(key, _r)| key.ident.name == following_seg.ident.name)
} else {
false
}
@ -747,11 +750,15 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
}
// Try to find in last block rib
if let Some(rib) = &self.last_block_rib && let RibKind::Normal = rib.kind {
if let Some(rib) = &self.last_block_rib
&& let RibKind::Normal = rib.kind
{
for (ident, &res) in &rib.bindings {
if let Res::Local(_) = res && path.len() == 1 &&
ident.span.eq_ctxt(path[0].ident.span) &&
ident.name == path[0].ident.name {
if let Res::Local(_) = res
&& path.len() == 1
&& ident.span.eq_ctxt(path[0].ident.span)
&& ident.name == path[0].ident.name
{
err.span_help(
ident.span,
format!("the binding `{path_str}` is available in a different scope in the same function"),
@ -867,9 +874,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
// (could be in a different file) or introduced in the same file as the typo
// (could belong to a different crate)
if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg
&& res
.opt_def_id()
.is_some_and(|id| id.is_local() || is_in_same_file(span, sugg_span))
&& res.opt_def_id().is_some_and(|id| id.is_local() || is_in_same_file(span, sugg_span))
{
err.span_label(
sugg_span,
@ -1074,12 +1079,11 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
&& trait_ref.path.span == span
&& let PathSource::Trait(_) = source
&& let Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) = res
&& let Ok(self_ty_str) =
self.r.tcx.sess.source_map().span_to_snippet(self_ty.span)
&& let Ok(self_ty_str) = self.r.tcx.sess.source_map().span_to_snippet(self_ty.span)
&& let Ok(trait_ref_str) =
self.r.tcx.sess.source_map().span_to_snippet(trait_ref.path.span)
{
err.multipart_suggestion(
err.multipart_suggestion(
"`impl` items mention the trait being implemented first and the type it is being implemented for second",
vec![(trait_ref.path.span, self_ty_str), (self_ty.span, trait_ref_str)],
Applicability::MaybeIncorrect,
@ -1106,12 +1110,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
source: PathSource<'_>,
span: Span,
) -> bool {
if let PathSource::Expr(_) = source &&
let Some(Expr {
span: expr_span,
kind: ExprKind::Assign(lhs, _, _),
..
}) = self.diagnostic_metadata.in_if_condition {
if let PathSource::Expr(_) = source
&& let Some(Expr { span: expr_span, kind: ExprKind::Assign(lhs, _, _), .. }) =
self.diagnostic_metadata.in_if_condition
{
// Icky heuristic so we don't suggest:
// `if (i + 2) = 2` => `if let (i + 2) = 2` (approximately pattern)
// `if 2 = i` => `if let 2 = i` (lhs needs to contain error span)
@ -1240,31 +1242,32 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
let mut has_self_arg = None;
if let PathSource::Expr(Some(parent)) = source
&& let ExprKind::Call(_, args) = &parent.kind
&& !args.is_empty() {
let mut expr_kind = &args[0].kind;
loop {
match expr_kind {
ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
if arg_name.segments[0].ident.name == kw::SelfLower {
let call_span = parent.span;
let tail_args_span = if args.len() > 1 {
Some(Span::new(
args[1].span.lo(),
args.last().unwrap().span.hi(),
call_span.ctxt(),
None,
))
} else {
None
};
has_self_arg = Some((call_span, tail_args_span));
}
break;
&& !args.is_empty()
{
let mut expr_kind = &args[0].kind;
loop {
match expr_kind {
ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
if arg_name.segments[0].ident.name == kw::SelfLower {
let call_span = parent.span;
let tail_args_span = if args.len() > 1 {
Some(Span::new(
args[1].span.lo(),
args.last().unwrap().span.hi(),
call_span.ctxt(),
None,
))
} else {
None
};
has_self_arg = Some((call_span, tail_args_span));
}
ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
_ => break,
break;
}
ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
_ => break,
}
}
}
has_self_arg
}
@ -1321,8 +1324,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
);
true
} else if kind == DefKind::Struct
&& let Some(lhs_source_span) = lhs_span.find_ancestor_inside(expr.span)
&& let Ok(snippet) = self.r.tcx.sess.source_map().span_to_snippet(lhs_source_span)
&& let Some(lhs_source_span) = lhs_span.find_ancestor_inside(expr.span)
&& let Ok(snippet) = self.r.tcx.sess.source_map().span_to_snippet(lhs_source_span)
{
// The LHS is a type that originates from a macro call.
// We have to add angle brackets around it.
@ -1427,7 +1430,13 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
.map(|(idx, new)| (new, old_fields.get(idx)))
.map(|(new, old)| {
let new = new.to_ident_string();
if let Some(Some(old)) = old && new != *old { format!("{new}: {old}") } else { new }
if let Some(Some(old)) = old
&& new != *old
{
format!("{new}: {old}")
} else {
new
}
})
.collect::<Vec<String>>()
} else {
@ -1813,7 +1822,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
}
}
if let RibKind::MacroDefinition(def) = rib.kind && def == self.r.macro_def(ctxt) {
if let RibKind::MacroDefinition(def) = rib.kind
&& def == self.r.macro_def(ctxt)
{
// If an invocation of this macro created `ident`, give up on `ident`
// and switch to `ident`'s source from the macro definition.
ctxt.remove_mark();
@ -1934,18 +1945,20 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
// try to give a suggestion for this pattern: `name = blah`, which is common in other languages
// suggest `let name = blah` to introduce a new binding
fn let_binding_suggestion(&mut self, err: &mut Diagnostic, ident_span: Span) -> bool {
if let Some(Expr { kind: ExprKind::Assign(lhs, .. ), .. }) = self.diagnostic_metadata.in_assignment &&
let ast::ExprKind::Path(None, _) = lhs.kind {
if !ident_span.from_expansion() {
err.span_suggestion_verbose(
ident_span.shrink_to_lo(),
"you might have meant to introduce a new binding",
"let ".to_string(),
Applicability::MaybeIncorrect,
);
return true;
}
if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) =
self.diagnostic_metadata.in_assignment
&& let ast::ExprKind::Path(None, _) = lhs.kind
{
if !ident_span.from_expansion() {
err.span_suggestion_verbose(
ident_span.shrink_to_lo(),
"you might have meant to introduce a new binding",
"let ".to_string(),
Applicability::MaybeIncorrect,
);
return true;
}
}
false
}
@ -2406,7 +2419,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
continue;
}
if !span.can_be_used_for_suggestions() && suggest_note && let Some(name) = name {
if !span.can_be_used_for_suggestions()
&& suggest_note
&& let Some(name) = name
{
suggest_note = false; // Avoid displaying the same help multiple times.
err.span_label(
span,

View file

@ -826,8 +826,10 @@ impl<'a> NameBindingData<'a> {
matches!(import.kind, ImportKind::ExternCrate { .. })
}
NameBindingKind::Module(module)
if let ModuleKind::Def(DefKind::Mod, def_id, _) = module.kind
=> def_id.is_crate_root(),
if let ModuleKind::Def(DefKind::Mod, def_id, _) = module.kind =>
{
def_id.is_crate_root()
}
_ => false,
}
}

View file

@ -748,33 +748,45 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
let mut suggestion = None;
let (span, label, module) = if let PathResult::Failed { span, label, module, .. } = path_res {
// try to suggest if it's not a macro, maybe a function
if let PathResult::NonModule(partial_res) = self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope)
&& partial_res.unresolved_segments() == 0 {
let sm = self.tcx.sess.source_map();
let exclamation_span = sm.next_point(span);
suggestion = Some((
vec![(exclamation_span, "".to_string())],
format!("{} is not a macro, but a {}, try to remove `!`", Segment::names_to_string(&path), partial_res.base_res().descr()),
Applicability::MaybeIncorrect
let (span, label, module) =
if let PathResult::Failed { span, label, module, .. } = path_res {
// try to suggest if it's not a macro, maybe a function
if let PathResult::NonModule(partial_res) =
self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope)
&& partial_res.unresolved_segments() == 0
{
let sm = self.tcx.sess.source_map();
let exclamation_span = sm.next_point(span);
suggestion = Some((
vec![(exclamation_span, "".to_string())],
format!(
"{} is not a macro, but a {}, try to remove `!`",
Segment::names_to_string(&path),
partial_res.base_res().descr()
),
Applicability::MaybeIncorrect,
));
}
(span, label, module)
} else {
(
path_span,
format!(
"partially resolved path in {} {}",
kind.article(),
kind.descr()
),
None,
)
};
}
(span, label, module)
} else {
(
path_span,
format!(
"partially resolved path in {} {}",
kind.article(),
kind.descr()
),
None,
)
};
self.report_error(
span,
ResolutionError::FailedToResolve { last_segment: path.last().map(|segment| segment.ident.name), label, suggestion, module },
ResolutionError::FailedToResolve {
last_segment: path.last().map(|segment| segment.ident.name),
label,
suggestion,
module,
},
);
}
PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),

View file

@ -346,7 +346,9 @@ pub fn has_primitive_or_keyword_docs(attrs: &[ast::Attribute]) -> bool {
for attr in attrs {
if attr.has_name(sym::rustc_doc_primitive) {
return true;
} else if attr.has_name(sym::doc) && let Some(items) = attr.meta_item_list() {
} else if attr.has_name(sym::doc)
&& let Some(items) = attr.meta_item_list()
{
for item in items {
if item.has_name(sym::keyword) {
return true;