Skip to content

Commit 9dcbc24

Browse files
authored
Unrolled build for rust-lang#134363
Rollup merge of rust-lang#134363 - estebank:derive-default, r=SparrowLii Use `#[derive(Default)]` instead of manual `impl` when possible While working on rust-lang#134175 I noticed a few manual `Default` `impl`s that could be `derive`d instead. These likely predate the existence of the `#[default]` attribute for `enum`s.
2 parents 904d8f6 + 1f82b45 commit 9dcbc24

File tree

8 files changed

+16
-77
lines changed

8 files changed

+16
-77
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+3-16
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_data_structures::sync::Lrc;
3131
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
3232
pub use rustc_span::AttrId;
3333
use rustc_span::source_map::{Spanned, respan};
34-
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
34+
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
3535
use thin_vec::{ThinVec, thin_vec};
3636

3737
pub use crate::format::*;
@@ -387,22 +387,15 @@ impl GenericParam {
387387

388388
/// Represents lifetime, type and const parameters attached to a declaration of
389389
/// a function, enum, trait, etc.
390-
#[derive(Clone, Encodable, Decodable, Debug)]
390+
#[derive(Clone, Encodable, Decodable, Debug, Default)]
391391
pub struct Generics {
392392
pub params: ThinVec<GenericParam>,
393393
pub where_clause: WhereClause,
394394
pub span: Span,
395395
}
396396

397-
impl Default for Generics {
398-
/// Creates an instance of `Generics`.
399-
fn default() -> Generics {
400-
Generics { params: ThinVec::new(), where_clause: Default::default(), span: DUMMY_SP }
401-
}
402-
}
403-
404397
/// A where-clause in a definition.
405-
#[derive(Clone, Encodable, Decodable, Debug)]
398+
#[derive(Clone, Encodable, Decodable, Debug, Default)]
406399
pub struct WhereClause {
407400
/// `true` if we ate a `where` token.
408401
///
@@ -419,12 +412,6 @@ impl WhereClause {
419412
}
420413
}
421414

422-
impl Default for WhereClause {
423-
fn default() -> WhereClause {
424-
WhereClause { has_where_token: false, predicates: ThinVec::new(), span: DUMMY_SP }
425-
}
426-
}
427-
428415
/// A single predicate in a where-clause.
429416
#[derive(Clone, Encodable, Decodable, Debug)]
430417
pub struct WherePredicate {

Diff for: compiler/rustc_ast_pretty/src/pprust/state/fixup.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use rustc_ast::Expr;
22
use rustc_ast::util::{classify, parser};
33

4-
#[derive(Copy, Clone, Debug)]
4+
// The default amount of fixing is minimal fixing, so all fixups are set to `false` by `Default`.
5+
// Fixups should be turned on in a targeted fashion where needed.
6+
#[derive(Copy, Clone, Debug, Default)]
57
pub(crate) struct FixupContext {
68
/// Print expression such that it can be parsed back as a statement
79
/// consisting of the original expression.
@@ -93,20 +95,6 @@ pub(crate) struct FixupContext {
9395
parenthesize_exterior_struct_lit: bool,
9496
}
9597

96-
/// The default amount of fixing is minimal fixing. Fixups should be turned on
97-
/// in a targeted fashion where needed.
98-
impl Default for FixupContext {
99-
fn default() -> Self {
100-
FixupContext {
101-
stmt: false,
102-
leftmost_subexpression_in_stmt: false,
103-
match_arm: false,
104-
leftmost_subexpression_in_match_arm: false,
105-
parenthesize_exterior_struct_lit: false,
106-
}
107-
}
108-
}
109-
11098
impl FixupContext {
11199
/// Create the initial fixup for printing an expression in statement
112100
/// position.

Diff for: compiler/rustc_lint/src/unused.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1023,19 +1023,14 @@ declare_lint! {
10231023
"`if`, `match`, `while` and `return` do not need parentheses"
10241024
}
10251025

1026+
#[derive(Default)]
10261027
pub(crate) struct UnusedParens {
10271028
with_self_ty_parens: bool,
10281029
/// `1 as (i32) < 2` parses to ExprKind::Lt
10291030
/// `1 as i32 < 2` parses to i32::<2[missing angle bracket]
10301031
parens_in_cast_in_lt: Vec<ast::NodeId>,
10311032
}
10321033

1033-
impl Default for UnusedParens {
1034-
fn default() -> Self {
1035-
Self { with_self_ty_parens: false, parens_in_cast_in_lt: Vec::new() }
1036-
}
1037-
}
1038-
10391034
impl_lint_pass!(UnusedParens => [UNUSED_PARENS]);
10401035

10411036
impl UnusedDelimLint for UnusedParens {

Diff for: compiler/rustc_session/src/config.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ pub struct CoverageOptions {
168168
}
169169

170170
/// Controls whether branch coverage or MC/DC coverage is enabled.
171-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
171+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
172172
pub enum CoverageLevel {
173173
/// Instrument for coverage at the MIR block level.
174+
#[default]
174175
Block,
175176
/// Also instrument branch points (includes block coverage).
176177
Branch,
@@ -195,12 +196,6 @@ pub enum CoverageLevel {
195196
Mcdc,
196197
}
197198

198-
impl Default for CoverageLevel {
199-
fn default() -> Self {
200-
Self::Block
201-
}
202-
}
203-
204199
/// Settings for `-Z instrument-xray` flag.
205200
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
206201
pub struct InstrumentXRay {

Diff for: library/core/tests/hash/mod.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ use std::hash::{BuildHasher, Hash, Hasher};
44
use std::ptr;
55
use std::rc::Rc;
66

7+
#[derive(Default)]
78
struct MyHasher {
89
hash: u64,
910
}
1011

11-
impl Default for MyHasher {
12-
fn default() -> MyHasher {
13-
MyHasher { hash: 0 }
14-
}
15-
}
16-
1712
impl Hasher for MyHasher {
1813
fn write(&mut self, buf: &[u8]) {
1914
for byte in buf {
@@ -107,6 +102,8 @@ fn test_writer_hasher() {
107102
struct Custom {
108103
hash: u64,
109104
}
105+
106+
#[derive(Default)]
110107
struct CustomHasher {
111108
output: u64,
112109
}
@@ -123,12 +120,6 @@ impl Hasher for CustomHasher {
123120
}
124121
}
125122

126-
impl Default for CustomHasher {
127-
fn default() -> CustomHasher {
128-
CustomHasher { output: 0 }
129-
}
130-
}
131-
132123
impl Hash for Custom {
133124
fn hash<H: Hasher>(&self, state: &mut H) {
134125
state.write_u64(self.hash);

Diff for: library/proc_macro/src/bridge/fxhash.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
2222
/// out-performs an FNV-based hash within rustc itself -- the collision rate is
2323
/// similar or slightly worse than FNV, but the speed of the hash function
2424
/// itself is much higher because it works on up to 8 bytes at a time.
25+
#[derive(Default)]
2526
pub struct FxHasher {
2627
hash: usize,
2728
}
@@ -31,13 +32,6 @@ const K: usize = 0x9e3779b9;
3132
#[cfg(target_pointer_width = "64")]
3233
const K: usize = 0x517cc1b727220a95;
3334

34-
impl Default for FxHasher {
35-
#[inline]
36-
fn default() -> FxHasher {
37-
FxHasher { hash: 0 }
38-
}
39-
}
40-
4135
impl FxHasher {
4236
#[inline]
4337
fn add_to_hash(&mut self, i: usize) {

Diff for: library/std/src/panicking.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ extern "C" fn __rust_foreign_exception() -> ! {
8181
rtabort!("Rust cannot catch foreign exceptions");
8282
}
8383

84+
#[derive(Default)]
8485
enum Hook {
86+
#[default]
8587
Default,
8688
Custom(Box<dyn Fn(&PanicHookInfo<'_>) + 'static + Sync + Send>),
8789
}
@@ -96,13 +98,6 @@ impl Hook {
9698
}
9799
}
98100

99-
impl Default for Hook {
100-
#[inline]
101-
fn default() -> Hook {
102-
Hook::Default
103-
}
104-
}
105-
106101
static HOOK: RwLock<Hook> = RwLock::new(Hook::Default);
107102

108103
/// Registers a custom panic hook, replacing the previously registered hook.

Diff for: library/std/src/sys_common/process.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,13 @@ use crate::sys::process::{EnvKey, ExitStatus, Process, StdioPipes};
88
use crate::{env, fmt, io};
99

1010
// Stores a set of changes to an environment
11-
#[derive(Clone)]
11+
#[derive(Clone, Default)]
1212
pub struct CommandEnv {
1313
clear: bool,
1414
saw_path: bool,
1515
vars: BTreeMap<EnvKey, Option<OsString>>,
1616
}
1717

18-
impl Default for CommandEnv {
19-
fn default() -> Self {
20-
CommandEnv { clear: false, saw_path: false, vars: Default::default() }
21-
}
22-
}
23-
2418
impl fmt::Debug for CommandEnv {
2519
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2620
let mut debug_command_env = f.debug_struct("CommandEnv");

0 commit comments

Comments
 (0)