1
Fork 0

Auto merge of #134443 - joshtriplett:use-field-init-shorthand, r=lqd,tgross35,nnethercote

Use field init shorthand where possible

Field init shorthand allows writing initializers like `tcx: tcx` as
`tcx`. The compiler already uses it extensively. Fix the last few places
where it isn't yet used.

EDIT: this PR also updates `rustfmt.toml` to set
`use_field_init_shorthand = true`.
This commit is contained in:
bors 2024-12-18 19:16:15 +00:00
commit 4ba4ac612d
17 changed files with 17 additions and 16 deletions

View file

@ -325,7 +325,7 @@ where
let actual_to = if from_end { let actual_to = if from_end {
if from.checked_add(to).is_none_or(|to| to > len) { if from.checked_add(to).is_none_or(|to| to > len) {
// This can only be reached in ConstProp and non-rustc-MIR. // This can only be reached in ConstProp and non-rustc-MIR.
throw_ub!(BoundsCheckFailed { len: len, index: from.saturating_add(to) }); throw_ub!(BoundsCheckFailed { len, index: from.saturating_add(to) });
} }
len.checked_sub(to).unwrap() len.checked_sub(to).unwrap()
} else { } else {

View file

@ -177,7 +177,7 @@ fn check_fn(tcx: TyCtxt<'_>, parent_def_id: LocalDefId) {
// Lazily compute these two, since they're likely a bit expensive. // Lazily compute these two, since they're likely a bit expensive.
variances: LazyCell::new(|| { variances: LazyCell::new(|| {
let mut functional_variances = FunctionalVariances { let mut functional_variances = FunctionalVariances {
tcx: tcx, tcx,
variances: FxHashMap::default(), variances: FxHashMap::default(),
ambient_variance: ty::Covariant, ambient_variance: ty::Covariant,
generics: tcx.generics_of(parent_def_id), generics: tcx.generics_of(parent_def_id),

View file

@ -1647,7 +1647,7 @@ impl<'a> Parser<'a> {
ident: prev_field, ident: prev_field,
}) })
} else { } else {
self.dcx().create_err(AtInStructPattern { span: span }) self.dcx().create_err(AtInStructPattern { span })
} }
} }

View file

@ -72,7 +72,7 @@ impl<I: Interner> Elaboratable<I> for ClauseWithSupertraitSpan<I> {
_parent_trait_pred: crate::Binder<I, crate::TraitPredicate<I>>, _parent_trait_pred: crate::Binder<I, crate::TraitPredicate<I>>,
_index: usize, _index: usize,
) -> Self { ) -> Self {
ClauseWithSupertraitSpan { pred: clause.as_predicate(), supertrait_span: supertrait_span } ClauseWithSupertraitSpan { pred: clause.as_predicate(), supertrait_span }
} }
} }

View file

@ -795,7 +795,7 @@ impl<T, A: Allocator> Rc<T, A> {
let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into(); let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into();
let init_ptr: NonNull<RcInner<T>> = uninit_ptr.cast(); let init_ptr: NonNull<RcInner<T>> = uninit_ptr.cast();
let weak = Weak { ptr: init_ptr, alloc: alloc }; let weak = Weak { ptr: init_ptr, alloc };
// It's important we don't give up ownership of the weak pointer, or // It's important we don't give up ownership of the weak pointer, or
// else the memory might be freed by the time `data_fn` returns. If // else the memory might be freed by the time `data_fn` returns. If

View file

@ -784,7 +784,7 @@ impl<T, A: Allocator> Arc<T, A> {
let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into(); let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into();
let init_ptr: NonNull<ArcInner<T>> = uninit_ptr.cast(); let init_ptr: NonNull<ArcInner<T>> = uninit_ptr.cast();
let weak = Weak { ptr: init_ptr, alloc: alloc }; let weak = Weak { ptr: init_ptr, alloc };
// It's important we don't give up ownership of the weak pointer, or // It's important we don't give up ownership of the weak pointer, or
// else the memory might be freed by the time `data_fn` returns. If // else the memory might be freed by the time `data_fn` returns. If

View file

@ -322,7 +322,7 @@ impl<'a> ContextBuilder<'a> {
// SAFETY: LocalWaker is just Waker without thread safety // SAFETY: LocalWaker is just Waker without thread safety
let local_waker = unsafe { transmute(waker) }; let local_waker = unsafe { transmute(waker) };
Self { Self {
waker: waker, waker,
local_waker, local_waker,
ext: ExtData::None(()), ext: ExtData::None(()),
_marker: PhantomData, _marker: PhantomData,

View file

@ -135,7 +135,7 @@ impl FileAttr {
S_IFREG => DT_REG, S_IFREG => DT_REG,
_ => DT_UNKNOWN, _ => DT_UNKNOWN,
}; };
FileType { mode: mode } FileType { mode }
} }
} }

View file

@ -43,7 +43,7 @@ impl Thread {
} }
Err(io::const_error!(io::ErrorKind::Uncategorized, "Unable to create thread!")) Err(io::const_error!(io::ErrorKind::Uncategorized, "Unable to create thread!"))
} else { } else {
Ok(Thread { tid: tid }) Ok(Thread { tid })
}; };
extern "C" fn thread_start(main: usize) { extern "C" fn thread_start(main: usize) {

View file

@ -22,7 +22,7 @@ impl Timespec {
const fn new(tv_sec: i64, tv_nsec: i32) -> Timespec { const fn new(tv_sec: i64, tv_nsec: i32) -> Timespec {
assert!(tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC); assert!(tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC);
// SAFETY: The assert above checks tv_nsec is within the valid range // SAFETY: The assert above checks tv_nsec is within the valid range
Timespec { t: timespec { tv_sec: tv_sec, tv_nsec: tv_nsec } } Timespec { t: timespec { tv_sec, tv_nsec } }
} }
fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> { fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {

View file

@ -12,7 +12,7 @@ pub struct FileDesc {
impl FileDesc { impl FileDesc {
pub fn new(fd: Fd) -> FileDesc { pub fn new(fd: Fd) -> FileDesc {
FileDesc { fd: fd } FileDesc { fd }
} }
pub fn raw(&self) -> Fd { pub fn raw(&self) -> Fd {

View file

@ -4,6 +4,7 @@ use_small_heuristics = "Max"
merge_derives = false merge_derives = false
group_imports = "StdExternalCrate" group_imports = "StdExternalCrate"
imports_granularity = "Module" imports_granularity = "Module"
use_field_init_shorthand = true
# Files to ignore. Each entry uses gitignore syntax, but `!` prefixes aren't allowed. # Files to ignore. Each entry uses gitignore syntax, but `!` prefixes aren't allowed.
ignore = [ ignore = [

View file

@ -286,7 +286,7 @@ mod imp {
impl<'a> Pipe<'a> { impl<'a> Pipe<'a> {
unsafe fn new<P: IntoRawHandle>(p: P, dst: &'a mut Vec<u8>) -> Pipe<'a> { unsafe fn new<P: IntoRawHandle>(p: P, dst: &'a mut Vec<u8>) -> Pipe<'a> {
Pipe { Pipe {
dst: dst, dst,
pipe: NamedPipe::from_raw_handle(p.into_raw_handle()), pipe: NamedPipe::from_raw_handle(p.into_raw_handle()),
overlapped: Overlapped::zero(), overlapped: Overlapped::zero(),
done: false, done: false,

View file

@ -14,7 +14,7 @@ fn id<T>(x: T) -> T {
impl<T> Struct<T> { impl<T> Struct<T> {
fn new(x: T) -> Struct<T> { fn new(x: T) -> Struct<T> {
Struct { x: x, f: id } Struct { x, f: id }
} }
fn get<T2>(self, x: T2) -> (T, T2) { fn get<T2>(self, x: T2) -> (T, T2) {

View file

@ -9,7 +9,7 @@ struct Foo {
#[no_mangle] #[no_mangle]
// CHECK: memcpy // CHECK: memcpy
fn interior(x: Vec<i32>) -> Vec<i32> { fn interior(x: Vec<i32>) -> Vec<i32> {
let Foo { x } = Foo { x: x }; let Foo { x } = Foo { x };
x x
} }

View file

@ -126,7 +126,7 @@ extern "C" {
fn main() { fn main() {
let s = Rect { a: 553, b: 554, c: 555, d: 556 }; let s = Rect { a: 553, b: 554, c: 555, d: 556 };
let t = BiggerRect { s: s, a: 27834, b: 7657 }; let t = BiggerRect { s, a: 27834, b: 7657 };
let u = FloatRect { a: 3489, b: 3490, c: 8. }; let u = FloatRect { a: 3489, b: 3490, c: 8. };
let v = Huge { a: 5647, b: 5648, c: 5649, d: 5650, e: 5651 }; let v = Huge { a: 5647, b: 5648, c: 5649, d: 5650, e: 5651 };
let w = Huge64 { a: 1234, b: 1335, c: 1436, d: 1537, e: 1638 }; let w = Huge64 { a: 1234, b: 1335, c: 1436, d: 1537, e: 1638 };

View file

@ -4,7 +4,7 @@ pub struct Def {
impl Def { impl Def {
pub fn new(id: i32) -> Def { pub fn new(id: i32) -> Def {
Def { id: id } Def { id }
} }
} }