Auto merge of #113978 - matthiaskrgr:clippy_072023_style, r=fee1-dead

couple of clippy::style changes

    comparison_to_empty
    iter_nth_zero
    for_kv_map
    manual_next_back
    redundant_pattern

    get_first
    single_char_add_str
    unnecessary_mut_passed
    manual_map
    manual_is_ascii_check
This commit is contained in:
bors 2023-07-24 07:48:04 +00:00
commit 155a5c2862
13 changed files with 27 additions and 32 deletions

View File

@ -62,7 +62,7 @@ fn get_horizontal_trim(lines: &[&str], kind: CommentKind) -> Option<String> {
CommentKind::Block => { CommentKind::Block => {
// Whatever happens, we skip the first line. // Whatever happens, we skip the first line.
let mut i = lines let mut i = lines
.get(0) .first()
.map(|l| if l.trim_start().starts_with('*') { 0 } else { 1 }) .map(|l| if l.trim_start().starts_with('*') { 0 } else { 1 })
.unwrap_or(0); .unwrap_or(0);
let mut j = lines.len(); let mut j = lines.len();

View File

@ -286,7 +286,7 @@ pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
ExprKind::OffsetOf(container, fields) => hir::ExprKind::OffsetOf( ExprKind::OffsetOf(container, fields) => hir::ExprKind::OffsetOf(
self.lower_ty( self.lower_ty(
container, container,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::OffsetOf), &ImplTraitContext::Disallowed(ImplTraitPosition::OffsetOf),
), ),
self.arena.alloc_from_iter(fields.iter().map(|&ident| self.lower_ident(ident))), self.arena.alloc_from_iter(fields.iter().map(|&ident| self.lower_ident(ident))),
), ),

View File

@ -697,15 +697,15 @@ pub fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> St
write!(template, "{n}").unwrap(); write!(template, "{n}").unwrap();
if p.format_options != Default::default() || p.format_trait != FormatTrait::Display if p.format_options != Default::default() || p.format_trait != FormatTrait::Display
{ {
template.push_str(":"); template.push(':');
} }
if let Some(fill) = p.format_options.fill { if let Some(fill) = p.format_options.fill {
template.push(fill); template.push(fill);
} }
match p.format_options.alignment { match p.format_options.alignment {
Some(FormatAlignment::Left) => template.push_str("<"), Some(FormatAlignment::Left) => template.push('<'),
Some(FormatAlignment::Right) => template.push_str(">"), Some(FormatAlignment::Right) => template.push('>'),
Some(FormatAlignment::Center) => template.push_str("^"), Some(FormatAlignment::Center) => template.push('^'),
None => {} None => {}
} }
match p.format_options.sign { match p.format_options.sign {

View File

@ -268,11 +268,7 @@ pub fn remove(&mut self, key: &K) -> Option<V> {
pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)> { pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)> {
match self { match self {
SsoHashMap::Array(array) => { SsoHashMap::Array(array) => {
if let Some(index) = array.iter().position(|(k, _v)| k == key) { array.iter().position(|(k, _v)| k == key).map(|index| array.swap_remove(index))
Some(array.swap_remove(index))
} else {
None
}
} }
SsoHashMap::Map(map) => map.remove_entry(key), SsoHashMap::Map(map) => map.remove_entry(key),
} }

View File

@ -3013,8 +3013,7 @@ pub struct FieldDef<'hir> {
impl FieldDef<'_> { impl FieldDef<'_> {
// Still necessary in couple of places // Still necessary in couple of places
pub fn is_positional(&self) -> bool { pub fn is_positional(&self) -> bool {
let first = self.ident.as_str().as_bytes()[0]; self.ident.as_str().as_bytes()[0].is_ascii_digit()
(b'0'..=b'9').contains(&first)
} }
} }

View File

@ -621,7 +621,7 @@ fn annotate_expected_due_to_let_ty(
// is in a different line, so we point at both. // is in a different line, so we point at both.
err.span_label(secondary_span, "expected due to the type of this binding"); err.span_label(secondary_span, "expected due to the type of this binding");
err.span_label(primary_span, format!("expected due to this{post_message}")); err.span_label(primary_span, format!("expected due to this{post_message}"));
} else if post_message == "" { } else if post_message.is_empty() {
// We are pointing at either the assignment lhs or the binding def pattern. // We are pointing at either the assignment lhs or the binding def pattern.
err.span_label(primary_span, "expected due to the type of this binding"); err.span_label(primary_span, "expected due to the type of this binding");
} else { } else {

View File

@ -425,9 +425,11 @@ fn iterate_region_constraints(
} }
} }
} else { } else {
for (constraint, _origin) in &region_constraints.data().constraints { region_constraints
each_constraint(constraint) .data()
} .constraints
.keys()
.for_each(|constraint| each_constraint(constraint));
} }
} }

View File

@ -372,7 +372,7 @@ fn skip_ascii_whitespace<F>(chars: &mut Chars<'_>, start: usize, callback: &mut
callback(start..end, EscapeError::MultipleSkippedLinesWarning); callback(start..end, EscapeError::MultipleSkippedLinesWarning);
} }
let tail = &tail[first_non_space..]; let tail = &tail[first_non_space..];
if let Some(c) = tail.chars().nth(0) { if let Some(c) = tail.chars().next() {
if c.is_whitespace() { if c.is_whitespace() {
// For error reporting, we would like the span to contain the character that was not // For error reporting, we would like the span to contain the character that was not
// skipped. The +1 is necessary to account for the leading \ that started the escape. // skipped. The +1 is necessary to account for the leading \ that started the escape.

View File

@ -1041,10 +1041,7 @@ fn list(ns: &[usize]) -> String {
} }
elem(curr, curr_count); elem(curr, curr_count);
let mut s = "[".to_string(); format!("[{}]", v.join(", "))
s.push_str(&v.join(", "));
s.push_str("]");
s
} }
}; };
@ -1229,12 +1226,13 @@ fn dump_mono_items_stats<'tcx>(
// Gather instantiated mono items grouped by def_id // Gather instantiated mono items grouped by def_id
let mut items_per_def_id: FxHashMap<_, Vec<_>> = Default::default(); let mut items_per_def_id: FxHashMap<_, Vec<_>> = Default::default();
for cgu in codegen_units { for cgu in codegen_units {
for (&mono_item, _) in cgu.items() { cgu.items()
.keys()
// Avoid variable-sized compiler-generated shims // Avoid variable-sized compiler-generated shims
if mono_item.is_user_defined() { .filter(|mono_item| mono_item.is_user_defined())
.for_each(|mono_item| {
items_per_def_id.entry(mono_item.def_id()).or_default().push(mono_item); items_per_def_id.entry(mono_item.def_id()).or_default().push(mono_item);
} });
}
} }
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
@ -1287,7 +1285,7 @@ fn codegened_and_inlined_items(tcx: TyCtxt<'_>, (): ()) -> &DefIdSet {
let mut result = items.clone(); let mut result = items.clone();
for cgu in cgus { for cgu in cgus {
for (item, _) in cgu.items() { for item in cgu.items().keys() {
if let MonoItem::Fn(ref instance) = item { if let MonoItem::Fn(ref instance) = item {
let did = instance.def_id(); let did = instance.def_id();
if !visited.insert(did) { if !visited.insert(did) {

View File

@ -27,7 +27,7 @@ pub(crate) fn emit_unescape_error(
lit, span_with_quotes, mode, range, error lit, span_with_quotes, mode, range, error
); );
let last_char = || { let last_char = || {
let c = lit[range.clone()].chars().rev().next().unwrap(); let c = lit[range.clone()].chars().next_back().unwrap();
let span = span.with_lo(span.hi() - BytePos(c.len_utf8() as u32)); let span = span.with_lo(span.hi() - BytePos(c.len_utf8() as u32));
(c, span) (c, span)
}; };

View File

@ -400,7 +400,7 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
let _ = write!(s, "{}", name.len()); let _ = write!(s, "{}", name.len());
// Prepend a '_' if name starts with a digit or '_' // Prepend a '_' if name starts with a digit or '_'
if let Some(first) = name.as_bytes().get(0) { if let Some(first) = name.as_bytes().first() {
if first.is_ascii_digit() || *first == b'_' { if first.is_ascii_digit() || *first == b'_' {
s.push('_'); s.push('_');
} }

View File

@ -194,7 +194,7 @@ fn try_fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
mapped_regions, mapped_regions,
mapped_types, mapped_types,
mapped_consts, mapped_consts,
&mut self.universes, &self.universes,
result, result,
)) ))
} else { } else {
@ -224,7 +224,7 @@ fn try_fold_const(&mut self, ct: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Sel
mapped_regions, mapped_regions,
mapped_types, mapped_types,
mapped_consts, mapped_consts,
&mut self.universes, &self.universes,
result, result,
)) ))
} else { } else {

View File

@ -51,7 +51,7 @@ fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<'
// assumed_wf_types should include those of `Opaque<T>`, `Opaque<T>` itself // assumed_wf_types should include those of `Opaque<T>`, `Opaque<T>` itself
// and `&'static T`. // and `&'static T`.
DefKind::OpaqueTy => bug!("unimplemented implied bounds for nested opaque types"), DefKind::OpaqueTy => bug!("unimplemented implied bounds for nested opaque types"),
def_kind @ _ => { def_kind => {
bug!("unimplemented implied bounds for opaque types with parent {def_kind:?}") bug!("unimplemented implied bounds for opaque types with parent {def_kind:?}")
} }
}, },