Rollup merge of #128762 - fmease:use-more-slice-pats, r=compiler-errors
Use more slice patterns inside the compiler Nothing super noteworthy. Just replacing the common 'fragile' pattern of "length check followed by indexing or unwrap" with slice patterns for legibility and 'robustness'. r? ghost
This commit is contained in:
commit
32e0fe129d
40 changed files with 191 additions and 221 deletions
|
@ -445,8 +445,8 @@ impl<'a> PathSource<'a> {
|
|||
Some(ExprKind::Call(call_expr, _)) => match &call_expr.kind {
|
||||
// the case of `::some_crate()`
|
||||
ExprKind::Path(_, path)
|
||||
if path.segments.len() == 2
|
||||
&& path.segments[0].ident.name == kw::PathRoot =>
|
||||
if let [segment, _] = path.segments.as_slice()
|
||||
&& segment.ident.name == kw::PathRoot =>
|
||||
{
|
||||
"external crate"
|
||||
}
|
||||
|
@ -2396,15 +2396,14 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
|
|||
}
|
||||
|
||||
fn future_proof_import(&mut self, use_tree: &UseTree) {
|
||||
let segments = &use_tree.prefix.segments;
|
||||
if !segments.is_empty() {
|
||||
let ident = segments[0].ident;
|
||||
if let [segment, rest @ ..] = use_tree.prefix.segments.as_slice() {
|
||||
let ident = segment.ident;
|
||||
if ident.is_path_segment_keyword() || ident.span.is_rust_2015() {
|
||||
return;
|
||||
}
|
||||
|
||||
let nss = match use_tree.kind {
|
||||
UseTreeKind::Simple(..) if segments.len() == 1 => &[TypeNS, ValueNS][..],
|
||||
UseTreeKind::Simple(..) if rest.is_empty() => &[TypeNS, ValueNS][..],
|
||||
_ => &[TypeNS],
|
||||
};
|
||||
let report_error = |this: &Self, ns| {
|
||||
|
@ -4009,16 +4008,15 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
|
|||
|
||||
if this.should_report_errs() {
|
||||
if candidates.is_empty() {
|
||||
if path.len() == 2 && prefix_path.len() == 1 {
|
||||
if path.len() == 2
|
||||
&& let [segment] = prefix_path
|
||||
{
|
||||
// Delay to check whether methond name is an associated function or not
|
||||
// ```
|
||||
// let foo = Foo {};
|
||||
// foo::bar(); // possibly suggest to foo.bar();
|
||||
//```
|
||||
err.stash(
|
||||
prefix_path[0].ident.span,
|
||||
rustc_errors::StashKey::CallAssocMethod,
|
||||
);
|
||||
err.stash(segment.ident.span, rustc_errors::StashKey::CallAssocMethod);
|
||||
} else {
|
||||
// When there is no suggested imports, we can just emit the error
|
||||
// and suggestions immediately. Note that we bypass the usually error
|
||||
|
|
|
@ -650,14 +650,14 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
let typo_sugg = self
|
||||
.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected)
|
||||
.to_opt_suggestion();
|
||||
if path.len() == 1
|
||||
if let [segment] = path
|
||||
&& !matches!(source, PathSource::Delegation)
|
||||
&& self.self_type_is_available()
|
||||
{
|
||||
if let Some(candidate) =
|
||||
self.lookup_assoc_candidate(ident, ns, is_expected, source.is_call())
|
||||
{
|
||||
let self_is_available = self.self_value_is_available(path[0].ident.span);
|
||||
let self_is_available = self.self_value_is_available(segment.ident.span);
|
||||
// Account for `Foo { field }` when suggesting `self.field` so we result on
|
||||
// `Foo { field: self.field }`.
|
||||
let pre = match source {
|
||||
|
@ -665,7 +665,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
if expr
|
||||
.fields
|
||||
.iter()
|
||||
.any(|f| f.ident == path[0].ident && f.is_shorthand) =>
|
||||
.any(|f| f.ident == segment.ident && f.is_shorthand) =>
|
||||
{
|
||||
format!("{path_str}: ")
|
||||
}
|
||||
|
@ -1258,8 +1258,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
)
|
||||
})
|
||||
.collect();
|
||||
if targets.len() == 1 {
|
||||
let target = targets[0];
|
||||
if let [target] = targets.as_slice() {
|
||||
return Some(TypoSuggestion::single_item_from_ident(target.0.ident, target.1));
|
||||
}
|
||||
}
|
||||
|
@ -2105,8 +2104,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
filter_fn: &impl Fn(Res) -> bool,
|
||||
) -> TypoCandidate {
|
||||
let mut names = Vec::new();
|
||||
if path.len() == 1 {
|
||||
let mut ctxt = path.last().unwrap().ident.span.ctxt();
|
||||
if let [segment] = path {
|
||||
let mut ctxt = segment.ident.span.ctxt();
|
||||
|
||||
// Search in lexical scope.
|
||||
// Walk backwards up the ribs in scope and collect candidates.
|
||||
|
|
|
@ -109,8 +109,8 @@ pub(crate) fn sub_namespace_match(
|
|||
// `format!("{}", path)`, because that tries to insert
|
||||
// line-breaks and is slow.
|
||||
fn fast_print_path(path: &ast::Path) -> Symbol {
|
||||
if path.segments.len() == 1 {
|
||||
path.segments[0].ident.name
|
||||
if let [segment] = path.segments.as_slice() {
|
||||
segment.ident.name
|
||||
} else {
|
||||
let mut path_str = String::with_capacity(64);
|
||||
for (i, segment) in path.segments.iter().enumerate() {
|
||||
|
@ -738,10 +738,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
|||
// Possibly apply the macro helper hack
|
||||
if deleg_impl.is_none()
|
||||
&& kind == Some(MacroKind::Bang)
|
||||
&& path.len() == 1
|
||||
&& path[0].ident.span.ctxt().outer_expn_data().local_inner_macros
|
||||
&& let [segment] = path.as_slice()
|
||||
&& segment.ident.span.ctxt().outer_expn_data().local_inner_macros
|
||||
{
|
||||
let root = Ident::new(kw::DollarCrate, path[0].ident.span);
|
||||
let root = Ident::new(kw::DollarCrate, segment.ident.span);
|
||||
path.insert(0, Segment::from_ident(root));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue