Skip to content

Commit d52f83b

Browse files
authored
Merge pull request #213 from rustcoreutils/clippy2
clippy-inspired cleanups
2 parents 36b49c6 + 6ad0148 commit d52f83b

File tree

45 files changed

+204
-201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+204
-201
lines changed

calc/expr.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,21 @@ fn cmpop(lhs: &Token, rhs: &Token, op: CmpOp) -> Result<Token, &'static str> {
185185
let lhs_int = token_to_int(lhs);
186186
let rhs_int = token_to_int(rhs);
187187

188-
// if both are integers, perform int comparison
189-
if lhs_int.is_some() && rhs_int.is_some() {
190-
Ok(cmpint(lhs_int.unwrap(), rhs_int.unwrap(), op))
191-
192-
// otherwise, convert int to string, and perform string compare
193-
} else if let Some(val) = lhs_int {
194-
let tmp = Token::Str(val.to_string());
195-
cmpstr(&tmp, rhs, op)
196-
} else if let Some(val) = rhs_int {
197-
let tmp = Token::Str(val.to_string());
198-
cmpstr(lhs, &tmp, op)
199-
} else {
200-
cmpstr(lhs, rhs, op)
188+
match (lhs_int, rhs_int) {
189+
(Some(lhs), Some(rhs)) => {
190+
// if both are integers, perform int comparison
191+
Ok(cmpint(lhs, rhs, op))
192+
}
193+
// otherwise, convert int to string, and perform string compare
194+
(Some(lh), _) => {
195+
let tmp = Token::Str(lh.to_string());
196+
cmpstr(&tmp, rhs, op)
197+
}
198+
(_, Some(rh)) => {
199+
let tmp = Token::Str(rh.to_string());
200+
cmpstr(lhs, &tmp, op)
201+
}
202+
_ => cmpstr(lhs, rhs, op),
201203
}
202204
}
203205

datetime/cal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
102102

103103
// If only one argument is provided, assume it is the entire year
104104
} else if args.month.is_some() && args.year.is_none() {
105-
args.year = args.month.clone();
105+
args.year = args.month;
106106
args.month = None;
107107
}
108108

datetime/date.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
149149
match &args.timestr {
150150
None => show_time(args.utc, DEF_TIMESTR),
151151
Some(timestr) => {
152-
if timestr.starts_with("+") {
153-
show_time(args.utc, &timestr[1..]);
152+
if let Some(st) = timestr.strip_prefix("+") {
153+
show_time(args.utc, st);
154154
} else {
155-
set_time(args.utc, &timestr)?;
155+
set_time(args.utc, timestr)?;
156156
}
157157
}
158158
}

dev/ar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl ArchiveMember {
234234
writer.write_all(&object::archive::TERMINATOR)?;
235235
writer.write_all(&self.data)?;
236236
if self.data.len() % 2 != 0 {
237-
writer.write_all(&[b'\n'])?;
237+
writer.write_all(b"\n")?;
238238
}
239239

240240
Ok(())

display/echo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn translate_str(skip_nl: bool, s: &str) -> String {
5555
output.push('\x11');
5656
}
5757
'\\' => {
58-
output.push_str("\\");
58+
output.push('\\');
5959
}
6060
_ => {}
6161
}
@@ -65,7 +65,7 @@ fn translate_str(skip_nl: bool, s: &str) -> String {
6565
}
6666

6767
if nl && !skip_nl {
68-
output.push_str("\n");
68+
output.push('\n');
6969
}
7070

7171
output

display/printf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn tokenize_format_str(format: &str) -> Vec<Token> {
126126
}
127127

128128
ParseState::Width => {
129-
if c.is_digit(10) {
129+
if c.is_ascii_digit() {
130130
width.push(c);
131131
done_with_char = true;
132132
} else {
@@ -148,7 +148,7 @@ fn tokenize_format_str(format: &str) -> Vec<Token> {
148148
}
149149

150150
ParseState::PrecisionValue => {
151-
if c.is_digit(10) {
151+
if c.is_ascii_digit() {
152152
precision.push(c);
153153
done_with_char = true;
154154
} else {

file/dd.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,16 @@ fn apply_conversions(data: &mut Vec<u8>, config: &Config) {
225225
}
226226

227227
fn copy_convert_file(config: &Config) -> Result<(), Box<dyn std::error::Error>> {
228-
let mut ifile: Box<dyn Read>;
229-
if config.ifile == "" {
230-
ifile = Box::new(io::stdin().lock());
228+
let mut ifile: Box<dyn Read> = if config.ifile.is_empty() {
229+
Box::new(io::stdin().lock())
231230
} else {
232-
ifile = Box::new(fs::File::open(&config.ifile)?);
233-
}
234-
let mut ofile: Box<dyn Write>;
235-
if config.ofile == "" {
236-
ofile = Box::new(io::stdout().lock())
231+
Box::new(fs::File::open(&config.ifile)?)
232+
};
233+
let mut ofile: Box<dyn Write> = if config.ofile.is_empty() {
234+
Box::new(io::stdout().lock())
237235
} else {
238-
ofile = Box::new(fs::File::create(&config.ofile)?)
239-
}
236+
Box::new(fs::File::create(&config.ofile)?)
237+
};
240238

241239
let mut ibuf = vec![0u8; config.ibs];
242240
let obuf = vec![0u8; config.obs];

file/file.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl Value {
156156
let re = Regex::new(r"\\([0-7]{1,3})").map_err(|_| RawMagicLineParseError::InvalidRegex)?;
157157

158158
let result = re
159-
.replace_all(&input, |capture: &regex::Captures| {
159+
.replace_all(input, |capture: &regex::Captures| {
160160
let mat = capture.get(1).unwrap().as_str();
161161

162162
let v = u32::from_str_radix(mat, 8).unwrap();
@@ -169,7 +169,7 @@ impl Value {
169169

170170
fn parse_number(input: &mut String) -> Option<(ComparisonOperator, u64)> {
171171
let regex = Regex::new(r"^[=<>&^x]").unwrap();
172-
let comparision_op = match regex.find(&input) {
172+
let comparision_op = match regex.find(input) {
173173
Some(mat) => {
174174
let comp = mat.as_str().chars().next().unwrap();
175175
input.replace_range(..1, ""); // Remove the matched operator
@@ -192,6 +192,8 @@ impl Value {
192192
}
193193

194194
/// Parses Hexadecimal, Octal and Unsigned Decimal
195+
// TODO
196+
#[allow(clippy::needless_match)]
195197
fn parse_number(input: &mut String) -> Option<u64> {
196198
if let Some(hex_num) = parse_hexadecimal(input) {
197199
Some(hex_num)
@@ -510,6 +512,7 @@ fn parse_magic_file_and_test(
510512
}
511513
}
512514
} else {
515+
// TODO: Empty branch
513516
}
514517
}
515518

file/find.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ fn parse_expression(tokens: &mut Vec<&str>) -> Vec<Expr> {
149149
"-size" => {
150150
tokens.pop();
151151
if let Some(size) = tokens.pop() {
152-
let (size, in_bytes) = if size.ends_with('c') {
153-
(size[..size.len() - 1].parse::<u64>().unwrap_or(0), true)
152+
let (size, in_bytes) = if let Some(st) = size.strip_suffix('c') {
153+
(st.parse::<u64>().unwrap_or(0), true)
154154
} else {
155155
(size.parse::<u64>().unwrap_or(0), false)
156156
};
@@ -250,15 +250,15 @@ fn evaluate_expression(
250250
match expression {
251251
Expr::Not(inner) => {
252252
let i: Vec<Expr> = vec![f_path.clone(), *inner.clone()];
253-
not_res = evaluate_expression(&i.as_slice(), files.clone(), root_dev)?;
253+
not_res = evaluate_expression(i.as_slice(), files.clone(), root_dev)?;
254254
}
255255
Expr::Or(inner) => {
256256
let i: Vec<Expr> = vec![f_path.clone(), *inner.clone()];
257-
or_res = evaluate_expression(&i.as_slice(), files.clone(), root_dev)?;
257+
or_res = evaluate_expression(i.as_slice(), files.clone(), root_dev)?;
258258
}
259259
Expr::And(inner) => {
260260
let i: Vec<Expr> = vec![f_path.clone(), *inner.clone()];
261-
and_res = evaluate_expression(&i.as_slice(), files.clone(), root_dev)?;
261+
and_res = evaluate_expression(i.as_slice(), files.clone(), root_dev)?;
262262
}
263263
_ => {}
264264
}
@@ -307,7 +307,7 @@ fn evaluate_expression(
307307
FileType::Fifo => file_type.is_fifo(),
308308
FileType::File => file_type.is_file(),
309309
FileType::Socket => file_type.is_socket(),
310-
FileType::Unknown => return Err(format!("Unknown argument to -type")),
310+
FileType::Unknown => return Err("Unknown argument to -type".to_string()),
311311
};
312312
if !r {
313313
c_files.remove(file.path());
@@ -316,15 +316,15 @@ fn evaluate_expression(
316316
Expr::NoUser => {
317317
if let Ok(metadata) = file.metadata() {
318318
let uid = metadata.uid();
319-
if !users::get_user_by_uid(uid).is_none() {
319+
if users::get_user_by_uid(uid).is_some() {
320320
c_files.remove(file.path());
321321
}
322322
}
323323
}
324324
Expr::NoGroup => {
325325
if let Ok(metadata) = file.metadata() {
326326
let gid = metadata.gid();
327-
if !users::get_group_by_gid(gid).is_none() {
327+
if users::get_group_by_gid(gid).is_some() {
328328
c_files.remove(file.path());
329329
}
330330
}

file/od.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,15 @@ fn parse_skip(offset: &str) -> Result<u64, Box<dyn std::error::Error>> {
177177
let (number, multiplier) = if offset.starts_with("0x") || offset.starts_with("0X") {
178178
// For hexadecimal, 'b' should be part of the number if it is the last character
179179
(offset, 1)
180-
} else if offset.ends_with('b') {
181-
(&offset[..offset.len() - 1], 512)
182-
} else if offset.ends_with('k') {
183-
(&offset[..offset.len() - 1], 1024)
184-
} else if offset.ends_with('m') {
185-
(&offset[..offset.len() - 1], 1048576)
186180
} else {
187-
(offset, 1)
181+
let mut chars = offset.chars();
182+
183+
match chars.next_back() {
184+
Some('b') => (chars.as_str(), 512),
185+
Some('k') => (chars.as_str(), 1024),
186+
Some('m') => (chars.as_str(), 1048576),
187+
_ => (offset, 1),
188+
}
188189
};
189190

190191
let base_value = parse_count::<u64>(number)?;

0 commit comments

Comments
 (0)