Skip to content

Commit 0a07286

Browse files
authored
Merge pull request #322 from fox0/default
Fix clippy::new_without_default
2 parents f9c35ea + ee1c2b9 commit 0a07286

File tree

8 files changed

+55
-135
lines changed

8 files changed

+55
-135
lines changed

display/printf.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ struct ConvSpec {
3131
zero_pad: bool,
3232
}
3333

34-
impl ConvSpec {
35-
fn new() -> ConvSpec {
36-
ConvSpec {
34+
impl Default for ConvSpec {
35+
fn default() -> Self {
36+
Self {
3737
spec: ' ',
38-
width: None,
39-
precision: None,
40-
left_justify: false,
41-
sign: false,
42-
space: false,
43-
alt_form: false,
44-
zero_pad: false,
38+
width: Default::default(),
39+
precision: Default::default(),
40+
left_justify: Default::default(),
41+
sign: Default::default(),
42+
space: Default::default(),
43+
alt_form: Default::default(),
44+
zero_pad: Default::default(),
4545
}
4646
}
4747
}
@@ -81,7 +81,7 @@ fn escaped_char(c: char) -> char {
8181
fn tokenize_format_str(format: &str) -> Vec<Token> {
8282
let mut tokens: Vec<Token> = Vec::new();
8383
let mut literal = String::with_capacity(format.len());
84-
let mut conversion = ConvSpec::new();
84+
let mut conversion = ConvSpec::default();
8585
let mut width = String::with_capacity(8);
8686
let mut precision = String::with_capacity(8);
8787
let mut state = ParseState::Literal;
@@ -164,7 +164,7 @@ fn tokenize_format_str(format: &str) -> Vec<Token> {
164164
ParseState::Specifier => {
165165
conversion.spec = c;
166166
tokens.push(Token::Conversion(conversion));
167-
conversion = ConvSpec::new();
167+
conversion = ConvSpec::default();
168168
state = ParseState::Literal;
169169
done_with_char = true;
170170
}

file/dd.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,20 @@ struct Config {
102102
notrunc: bool,
103103
}
104104

105-
impl Config {
106-
fn new() -> Config {
107-
Config {
108-
ifile: String::new(),
109-
ofile: String::new(),
105+
impl Default for Config {
106+
fn default() -> Self {
107+
Self {
108+
ifile: Default::default(),
109+
ofile: Default::default(),
110110
ibs: DEF_BLOCK_SIZE,
111111
obs: DEF_BLOCK_SIZE,
112-
cbs: 0,
113-
seek: 0,
114-
skip: 0,
115-
count: 0,
116-
conversions: Vec::new(),
117-
noerror: false,
118-
notrunc: false,
112+
cbs: Default::default(),
113+
seek: Default::default(),
114+
skip: Default::default(),
115+
count: Default::default(),
116+
conversions: Default::default(),
117+
noerror: Default::default(),
118+
notrunc: Default::default(),
119119
}
120120
}
121121
}
@@ -341,7 +341,7 @@ fn parse_block_size(s: &str) -> Result<usize, Box<dyn std::error::Error>> {
341341
}
342342

343343
fn parse_cmdline(args: &[String]) -> Result<Config, Box<dyn std::error::Error>> {
344-
let mut config = Config::new();
344+
let mut config = Config::default();
345345

346346
for arg in args {
347347
// Split arg into option and argument

file/tee.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,11 @@ struct TeeFile {
3232
f: File,
3333
}
3434

35+
#[derive(Default)]
3536
struct TeeInfo {
3637
outputs: Vec<TeeFile>,
3738
}
3839

39-
impl TeeInfo {
40-
fn new() -> TeeInfo {
41-
TeeInfo {
42-
outputs: Vec::new(),
43-
}
44-
}
45-
}
46-
4740
fn open_outputs(args: &Args, info: &mut TeeInfo) -> io::Result<()> {
4841
for filename in &args.files {
4942
let f_res = OpenOptions::new()
@@ -113,7 +106,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
113106
}
114107
}
115108

116-
let mut state = TeeInfo::new();
109+
let mut state = TeeInfo::default();
117110

118111
open_outputs(&args, &mut state)?;
119112
tee_stdin(&mut state)?;

plib/src/modestr.rs

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ use libc::{
1212
S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR,
1313
};
1414

15-
#[derive(PartialEq, Debug)]
15+
#[derive(PartialEq, Debug, Default)]
1616
pub enum ChmodActionOp {
1717
Add,
1818
Remove,
19+
#[default]
1920
Set,
2021
}
2122

22-
#[derive(Debug)]
23+
#[derive(Debug, Default)]
2324
pub struct ChmodAction {
2425
pub op: ChmodActionOp,
2526

@@ -37,25 +38,7 @@ pub struct ChmodAction {
3738
dirty: bool,
3839
}
3940

40-
impl ChmodAction {
41-
pub fn new() -> ChmodAction {
42-
ChmodAction {
43-
op: ChmodActionOp::Set,
44-
copy_user: false,
45-
copy_group: false,
46-
copy_others: false,
47-
read: false,
48-
write: false,
49-
execute: false,
50-
execute_dir: false,
51-
setuid: false,
52-
sticky: false,
53-
dirty: false,
54-
}
55-
}
56-
}
57-
58-
#[derive(Debug)]
41+
#[derive(Debug, Default)]
5942
pub struct ChmodClause {
6043
// wholist
6144
pub user: bool,
@@ -68,39 +51,20 @@ pub struct ChmodClause {
6851
dirty: bool,
6952
}
7053

71-
impl ChmodClause {
72-
pub fn new() -> ChmodClause {
73-
ChmodClause {
74-
user: false,
75-
group: false,
76-
others: false,
77-
actions: Vec::new(),
78-
dirty: false,
79-
}
80-
}
81-
}
82-
83-
#[derive(Debug)]
54+
#[derive(Debug, Default)]
8455
pub struct ChmodSymbolic {
8556
pub clauses: Vec<ChmodClause>,
8657
}
8758

88-
impl ChmodSymbolic {
89-
pub fn new() -> ChmodSymbolic {
90-
ChmodSymbolic {
91-
clauses: Vec::new(),
92-
}
93-
}
94-
}
95-
9659
#[derive(Debug)]
9760
pub enum ChmodMode {
9861
Absolute(u32),
9962
Symbolic(ChmodSymbolic),
10063
}
10164

102-
#[derive(Debug)]
65+
#[derive(Debug, Default)]
10366
enum ParseState {
67+
#[default]
10468
Wholist,
10569
Actionlist,
10670
ListOrCopy,
@@ -114,11 +78,11 @@ pub fn parse(mode: &str) -> Result<ChmodMode, String> {
11478
return Ok(ChmodMode::Absolute(m));
11579
}
11680

117-
let mut state = ParseState::Wholist;
11881
let mut done_with_char;
119-
let mut symbolic = ChmodSymbolic::new();
120-
let mut clause = ChmodClause::new();
121-
let mut action = ChmodAction::new();
82+
let mut state = ParseState::default();
83+
let mut symbolic = ChmodSymbolic::default();
84+
let mut clause = ChmodClause::default();
85+
let mut action = ChmodAction::default();
12286

12387
for c in mode.chars() {
12488
done_with_char = false;
@@ -156,7 +120,7 @@ pub fn parse(mode: &str) -> Result<ChmodMode, String> {
156120
action.dirty = false;
157121
done_with_char = false;
158122
symbolic.clauses.push(clause);
159-
clause = ChmodClause::new();
123+
clause = ChmodClause::default();
160124
state = ParseState::NextClause;
161125
}
162126
}
@@ -177,7 +141,7 @@ pub fn parse(mode: &str) -> Result<ChmodMode, String> {
177141
done_with_char = false;
178142
clause.actions.push(action);
179143
clause.dirty = true;
180-
action = ChmodAction::new();
144+
action = ChmodAction::default();
181145
state = ParseState::Actionlist;
182146
}
183147
}
@@ -196,7 +160,7 @@ pub fn parse(mode: &str) -> Result<ChmodMode, String> {
196160
done_with_char = false;
197161
clause.actions.push(action);
198162
clause.dirty = true;
199-
action = ChmodAction::new();
163+
action = ChmodAction::default();
200164
state = ParseState::Actionlist;
201165
}
202166
}

text/asa.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ struct AsaState {
3030
lines: Vec<String>,
3131
}
3232

33-
impl AsaState {
34-
fn new() -> AsaState {
35-
AsaState {
33+
impl Default for AsaState {
34+
fn default() -> Self {
35+
Self {
3636
first_line: true,
37-
lines: Vec::new(),
37+
lines: Default::default(),
3838
}
3939
}
40+
}
4041

42+
impl AsaState {
4143
fn push(&mut self, line: &str) {
4244
self.lines.push(line.to_string());
4345
if self.first_line {
@@ -69,7 +71,7 @@ impl AsaState {
6971
fn asa_file(pathname: &PathBuf) -> io::Result<()> {
7072
let mut reader = plib::io::input_reader(pathname, false)?;
7173
let mut line_no: usize = 0;
72-
let mut state = AsaState::new();
74+
let mut state = AsaState::default();
7375

7476
loop {
7577
line_no += 1;

text/sort.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Args {
121121
}
122122

123123
/// A struct representing a range field with various sorting and comparison options.
124-
#[derive(Clone)]
124+
#[derive(Clone, Default)]
125125
struct RangeField {
126126
/// The number of the field to be considered in the range.
127127
field_number: usize,
@@ -149,21 +149,6 @@ struct RangeField {
149149
dictionary_order: bool,
150150
}
151151

152-
impl RangeField {
153-
fn new() -> RangeField {
154-
Self {
155-
field_number: 0,
156-
first_character: 0,
157-
numeric_sort: false,
158-
ignore_leading_blanks: false,
159-
reverse: false,
160-
ignore_nonprintable: false,
161-
fold_case: false,
162-
dictionary_order: false,
163-
}
164-
}
165-
}
166-
167152
/// Updates two RangeField objects based on their comparison options.
168153
///
169154
/// This function takes two RangeField objects, compares their fields, and updates the fields
@@ -744,7 +729,7 @@ fn create_ranges(
744729
let mut key_ranges = key_ranges.iter();
745730

746731
// Convert key ranges to numeric representations
747-
let mut ranges: (RangeField, Option<RangeField>) = (RangeField::new(), None);
732+
let mut ranges: (RangeField, Option<RangeField>) = (RangeField::default(), None);
748733

749734
ranges.0 = {
750735
let key_range = key_ranges.next().unwrap().to_string();

tree/ls_util/entry.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ impl Entry {
464464
}
465465
}
466466

467-
// Used for padding in long format
467+
/// Used for padding in long format
468+
#[derive(Default)]
468469
pub struct LongFormatPadding {
469470
pub blocks_str_width: usize,
470471
pub inode_str_width: usize,
@@ -477,22 +478,6 @@ pub struct LongFormatPadding {
477478
pub time_width: usize,
478479
}
479480

480-
impl Default for LongFormatPadding {
481-
fn default() -> Self {
482-
Self {
483-
blocks_str_width: 0,
484-
inode_str_width: 0,
485-
num_links_width: 0,
486-
owner_name_width: 0,
487-
group_name_width: 0,
488-
file_size_width: 0,
489-
device_id_major_width: 0,
490-
device_id_minor_width: 0,
491-
time_width: 0,
492-
}
493-
}
494-
}
495-
496481
impl LongFormatPadding {
497482
/// Get the maximum padding for each field.
498483
pub fn update_maximum(&mut self, other: &LongFormatPadding) {
@@ -525,25 +510,15 @@ impl LongFormatPadding {
525510
}
526511
}
527512

528-
// Used for padding in multi-column format
513+
/// Used for padding in multi-column format
514+
#[derive(Default)]
529515
pub struct MultiColumnPadding {
530516
pub total_width: usize,
531517
pub inode_str_width: usize,
532518
pub blocks_str_width: usize,
533519
pub file_name_width: usize,
534520
}
535521

536-
impl Default for MultiColumnPadding {
537-
fn default() -> Self {
538-
Self {
539-
total_width: 0,
540-
inode_str_width: 0,
541-
blocks_str_width: 0,
542-
file_name_width: 0,
543-
}
544-
}
545-
}
546-
547522
impl MultiColumnPadding {
548523
pub fn update_maximum(&mut self, other: &MultiColumnPadding) {
549524
self.total_width = usize::max(self.total_width, other.total_width);

users/talk.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ pub struct Osockaddr {
207207
pub sa_data: [u8; 14],
208208
}
209209

210+
#[allow(clippy::derivable_impls)]
210211
impl Default for Osockaddr {
211212
fn default() -> Self {
212213
Osockaddr {

0 commit comments

Comments
 (0)