Skip to content

Commit

Permalink
Auto merge of #134443 - joshtriplett:use-field-init-shorthand, r=lqd,…
Browse files Browse the repository at this point in the history
…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`.
  • Loading branch information
bors committed Dec 18, 2024
2 parents a52085d + a105cd6 commit 4ba4ac6
Show file tree
Hide file tree
Showing 17 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ where
let actual_to = if from_end {
if from.checked_add(to).is_none_or(|to| to > len) {
// 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()
} else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/impl_trait_overcaptures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn check_fn(tcx: TyCtxt<'_>, parent_def_id: LocalDefId) {
// Lazily compute these two, since they're likely a bit expensive.
variances: LazyCell::new(|| {
let mut functional_variances = FunctionalVariances {
tcx: tcx,
tcx,
variances: FxHashMap::default(),
ambient_variance: ty::Covariant,
generics: tcx.generics_of(parent_def_id),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@ impl<'a> Parser<'a> {
ident: prev_field,
})
} else {
self.dcx().create_err(AtInStructPattern { span: span })
self.dcx().create_err(AtInStructPattern { span })
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_type_ir/src/elaborate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<I: Interner> Elaboratable<I> for ClauseWithSupertraitSpan<I> {
_parent_trait_pred: crate::Binder<I, crate::TraitPredicate<I>>,
_index: usize,
) -> Self {
ClauseWithSupertraitSpan { pred: clause.as_predicate(), supertrait_span: supertrait_span }
ClauseWithSupertraitSpan { pred: clause.as_predicate(), supertrait_span }
}
}

Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ impl<T, A: Allocator> Rc<T, A> {
let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into();
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
// else the memory might be freed by the time `data_fn` returns. If
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ impl<T, A: Allocator> Arc<T, A> {
let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into();
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
// else the memory might be freed by the time `data_fn` returns. If
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/task/wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl<'a> ContextBuilder<'a> {
// SAFETY: LocalWaker is just Waker without thread safety
let local_waker = unsafe { transmute(waker) };
Self {
waker: waker,
waker,
local_waker,
ext: ExtData::None(()),
_marker: PhantomData,
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/hermit/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl FileAttr {
S_IFREG => DT_REG,
_ => DT_UNKNOWN,
};
FileType { mode: mode }
FileType { mode }
}
}

Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/hermit/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Thread {
}
Err(io::const_error!(io::ErrorKind::Uncategorized, "Unable to create thread!"))
} else {
Ok(Thread { tid: tid })
Ok(Thread { tid })
};

extern "C" fn thread_start(main: usize) {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/hermit/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Timespec {
const fn new(tv_sec: i64, tv_nsec: i32) -> Timespec {
assert!(tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC);
// 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> {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/sgx/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct FileDesc {

impl FileDesc {
pub fn new(fd: Fd) -> FileDesc {
FileDesc { fd: fd }
FileDesc { fd }
}

pub fn raw(&self) -> Fd {
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use_small_heuristics = "Max"
merge_derives = false
group_imports = "StdExternalCrate"
imports_granularity = "Module"
use_field_init_shorthand = true

# Files to ignore. Each entry uses gitignore syntax, but `!` prefixes aren't allowed.
ignore = [
Expand Down
2 changes: 1 addition & 1 deletion src/tools/compiletest/src/read2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ mod imp {
impl<'a> Pipe<'a> {
unsafe fn new<P: IntoRawHandle>(p: P, dst: &'a mut Vec<u8>) -> Pipe<'a> {
Pipe {
dst: dst,
dst,
pipe: NamedPipe::from_raw_handle(p.into_raw_handle()),
overlapped: Overlapped::zero(),
done: false,
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen-units/item-collection/generic-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn id<T>(x: T) -> T {

impl<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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/issues/issue-15953.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct Foo {
#[no_mangle]
// CHECK: memcpy
fn interior(x: Vec<i32>) -> Vec<i32> {
let Foo { x } = Foo { x: x };
let Foo { x } = Foo { x };
x
}

Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/extern-fn-struct-passing-abi/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ extern "C" {

fn main() {
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 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 };
Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/symbols-include-type-name/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub struct Def {

impl Def {
pub fn new(id: i32) -> Def {
Def { id: id }
Def { id }
}
}

Expand Down

0 comments on commit 4ba4ac6

Please sign in to comment.