Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use #[derive(Default)] instead of manual impl when possible #134363

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use rustc_data_structures::sync::Lrc;
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
pub use rustc_span::AttrId;
use rustc_span::source_map::{Spanned, respan};
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
use thin_vec::{ThinVec, thin_vec};

pub use crate::format::*;
Expand Down Expand Up @@ -387,22 +387,15 @@ impl GenericParam {

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

impl Default for Generics {
/// Creates an instance of `Generics`.
fn default() -> Generics {
Generics { params: ThinVec::new(), where_clause: Default::default(), span: DUMMY_SP }
}
}

/// A where-clause in a definition.
#[derive(Clone, Encodable, Decodable, Debug)]
#[derive(Clone, Encodable, Decodable, Debug, Default)]
pub struct WhereClause {
/// `true` if we ate a `where` token.
///
Expand All @@ -419,12 +412,6 @@ impl WhereClause {
}
}

impl Default for WhereClause {
fn default() -> WhereClause {
WhereClause { has_where_token: false, predicates: ThinVec::new(), span: DUMMY_SP }
}
}

/// A single predicate in a where-clause.
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct WherePredicate {
Expand Down
18 changes: 3 additions & 15 deletions compiler/rustc_ast_pretty/src/pprust/state/fixup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use rustc_ast::Expr;
use rustc_ast::util::{classify, parser};

#[derive(Copy, Clone, Debug)]
// The default amount of fixing is minimal fixing, so all fixups are set to `false` by `Default`.
// Fixups should be turned on in a targeted fashion where needed.
#[derive(Copy, Clone, Debug, Default)]
estebank marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) struct FixupContext {
/// Print expression such that it can be parsed back as a statement
/// consisting of the original expression.
Expand Down Expand Up @@ -93,20 +95,6 @@ pub(crate) struct FixupContext {
parenthesize_exterior_struct_lit: bool,
}

/// The default amount of fixing is minimal fixing. Fixups should be turned on
/// in a targeted fashion where needed.
impl Default for FixupContext {
fn default() -> Self {
FixupContext {
stmt: false,
leftmost_subexpression_in_stmt: false,
match_arm: false,
leftmost_subexpression_in_match_arm: false,
parenthesize_exterior_struct_lit: false,
}
}
}

impl FixupContext {
/// Create the initial fixup for printing an expression in statement
/// position.
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,19 +1023,14 @@ declare_lint! {
"`if`, `match`, `while` and `return` do not need parentheses"
}

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

impl Default for UnusedParens {
fn default() -> Self {
Self { with_self_ty_parens: false, parens_in_cast_in_lt: Vec::new() }
}
}

impl_lint_pass!(UnusedParens => [UNUSED_PARENS]);

impl UnusedDelimLint for UnusedParens {
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ pub struct CoverageOptions {
}

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

impl Default for CoverageLevel {
fn default() -> Self {
Self::Block
}
}

/// Settings for `-Z instrument-xray` flag.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct InstrumentXRay {
Expand Down
15 changes: 3 additions & 12 deletions library/core/tests/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ use std::hash::{BuildHasher, Hash, Hasher};
use std::ptr;
use std::rc::Rc;

#[derive(Default)]
struct MyHasher {
hash: u64,
}

impl Default for MyHasher {
fn default() -> MyHasher {
MyHasher { hash: 0 }
}
}

impl Hasher for MyHasher {
fn write(&mut self, buf: &[u8]) {
for byte in buf {
Expand Down Expand Up @@ -107,6 +102,8 @@ fn test_writer_hasher() {
struct Custom {
hash: u64,
}

#[derive(Default)]
struct CustomHasher {
output: u64,
}
Expand All @@ -123,12 +120,6 @@ impl Hasher for CustomHasher {
}
}

impl Default for CustomHasher {
fn default() -> CustomHasher {
CustomHasher { output: 0 }
}
}

impl Hash for Custom {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u64(self.hash);
Expand Down
8 changes: 1 addition & 7 deletions library/proc_macro/src/bridge/fxhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
/// out-performs an FNV-based hash within rustc itself -- the collision rate is
/// similar or slightly worse than FNV, but the speed of the hash function
/// itself is much higher because it works on up to 8 bytes at a time.
#[derive(Default)]
pub struct FxHasher {
hash: usize,
}
Expand All @@ -31,13 +32,6 @@ const K: usize = 0x9e3779b9;
#[cfg(target_pointer_width = "64")]
const K: usize = 0x517cc1b727220a95;

impl Default for FxHasher {
#[inline]
fn default() -> FxHasher {
FxHasher { hash: 0 }
}
}

impl FxHasher {
#[inline]
fn add_to_hash(&mut self, i: usize) {
Expand Down
9 changes: 2 additions & 7 deletions library/std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ extern "C" fn __rust_foreign_exception() -> ! {
rtabort!("Rust cannot catch foreign exceptions");
}

#[derive(Default)]
enum Hook {
#[default]
Default,
Custom(Box<dyn Fn(&PanicHookInfo<'_>) + 'static + Sync + Send>),
}
Expand All @@ -96,13 +98,6 @@ impl Hook {
}
}

impl Default for Hook {
#[inline]
fn default() -> Hook {
Hook::Default
}
}

static HOOK: RwLock<Hook> = RwLock::new(Hook::Default);

/// Registers a custom panic hook, replacing the previously registered hook.
Expand Down
8 changes: 1 addition & 7 deletions library/std/src/sys_common/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@ use crate::sys::process::{EnvKey, ExitStatus, Process, StdioPipes};
use crate::{env, fmt, io};

// Stores a set of changes to an environment
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct CommandEnv {
clear: bool,
saw_path: bool,
vars: BTreeMap<EnvKey, Option<OsString>>,
}

impl Default for CommandEnv {
fn default() -> Self {
CommandEnv { clear: false, saw_path: false, vars: Default::default() }
}
}

impl fmt::Debug for CommandEnv {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut debug_command_env = f.debug_struct("CommandEnv");
Expand Down
Loading