Skip to content

Commit 4e708ef

Browse files
committed
sh: fix clippy warnings
1 parent 47936d3 commit 4e708ef

40 files changed

+157
-214
lines changed

sh/src/builtin/alias.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ impl BuiltinUtility for AliasBuiltin {
3434
shell
3535
.alias_table
3636
.insert(alias.to_string(), command.to_string());
37+
} else if let Some(command) = shell.alias_table.get(arg) {
38+
opened_files.write_out(format!("alias {}='{}'", arg, command));
3739
} else {
38-
if let Some(command) = shell.alias_table.get(arg) {
39-
opened_files.write_out(format!("alias {}='{}'", arg, command));
40-
} else {
41-
return Err(format!("alias: {}: not found", arg).into());
42-
}
40+
return Err(format!("alias: {}: not found", arg).into());
4341
}
4442
}
4543
Ok(0)

sh/src/builtin/bg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl BuiltinUtility for Bg {
5757
status = 1;
5858
}
5959
} else {
60-
opened_files.write_err("bg: no background jobs".to_string());
60+
opened_files.write_err("bg: no background jobs");
6161
status = 1;
6262
}
6363
} else {

sh/src/builtin/cd.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,12 @@ impl BuiltinUtility for Cd {
8686
} => {
8787
let dir = if let Some(dir) = directory {
8888
dir
89+
} else if let Some(home_dir) = shell.environment.get_str_value("HOME") {
90+
home_dir
8991
} else {
90-
if let Some(home_dir) = shell.environment.get_str_value("HOME") {
91-
home_dir
92-
} else {
93-
// behaviour is implementation defined, bash just returns 0
94-
// and doesn't change directory
95-
return Ok(0);
96-
}
92+
// behaviour is implementation defined, bash just returns 0
93+
// and doesn't change directory
94+
return Ok(0);
9795
};
9896
let mut curr_path = OsString::new();
9997

@@ -122,7 +120,7 @@ impl BuiltinUtility for Cd {
122120
}
123121

124122
if !handle_dot_dot_physically {
125-
if !curr_path.as_bytes().get(0).is_some_and(|c| *c == b'/') {
123+
if !curr_path.as_bytes().first().is_some_and(|c| *c == b'/') {
126124
let mut new_curr_path = shell
127125
.environment
128126
.get_str_value("PWD")

sh/src/builtin/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl BuiltinUtility for Command {
131131
args.command_name
132132
))
133133
}
134-
} else if shell.functions.get(args.command_name).is_some() {
134+
} else if shell.functions.contains_key(args.command_name) {
135135
if args.action == Action::PrintShort {
136136
opened_files.write_out(format!("{}\n", args.command_name))
137137
} else {

sh/src/builtin/control_flow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn loop_control_flow(
3030
if args.len() > 1 {
3131
return Err(format!("{name}: too many arguments").into());
3232
}
33-
let n = if let Some(n) = args.get(0) {
33+
let n = if let Some(n) = args.first() {
3434
match n.parse::<i32>() {
3535
Ok(n) => n,
3636
Err(_) => {
@@ -74,7 +74,7 @@ impl SpecialBuiltinUtility for Return {
7474
if args.len() > 1 {
7575
return Err("return: too many arguments".into());
7676
}
77-
let n = if let Some(n) = args.get(0) {
77+
let n = if let Some(n) = args.first() {
7878
match n.parse::<i32>() {
7979
Ok(n) => n,
8080
Err(_) => {

sh/src/builtin/exec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ impl SpecialBuiltinUtility for Exec {
3030
.find_command(&args[0], "", true)
3131
.ok_or(format!("exec: {}: command not found", args[0]))?;
3232

33-
shell.exec(command, &args, opened_files)
33+
shell.exec(command, args, opened_files)
3434
}
3535
}

sh/src/builtin/exit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl SpecialBuiltinUtility for Exit {
2020
return Err("exit: too many arguments".into());
2121
}
2222

23-
if let Some(arg) = args.get(0) {
23+
if let Some(arg) = args.first() {
2424
if let Ok(n) = arg.parse::<i32>() {
2525
shell.exit(n);
2626
} else {

sh/src/builtin/export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl SpecialBuiltinUtility for Export {
2121
shell: &mut Shell,
2222
opened_files: &mut OpenedFiles,
2323
) -> BuiltinResult {
24-
if args.get(0).is_some_and(|arg| arg == "-p") {
24+
if args.first().is_some_and(|arg| arg == "-p") {
2525
if args.len() > 1 && !(args.len() == 2 && args[1] == "--") {
2626
return Err("export: too many arguments".into());
2727
}
@@ -55,7 +55,7 @@ impl SpecialBuiltinUtility for Export {
5555
}
5656
(name.to_string(), Some(value[1..].to_string()))
5757
} else {
58-
if !is_valid_name(&arg) {
58+
if !is_valid_name(arg) {
5959
return Err(format!("export: '{arg}' is not a valid name\n").into());
6060
}
6161
(arg.clone(), None)

sh/src/builtin/fc.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ impl<'s> FcArgs<'s> {
112112
let mut replace = None;
113113
let mut first = None;
114114
if let Some(first_op) = args.get(first_operand) {
115-
if first_op.contains('=') {
116-
let mut split = first_op.splitn(2, '=');
117-
let old = split.next().unwrap();
118-
let new = split.next().unwrap();
115+
if let Some((old, new)) = first_op.split_once('=') {
119116
replace = Some(Replace { old, new });
120117
} else {
121118
first = Some(EndPoint::parse(first_op));

sh/src/builtin/fg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn run_foreground_job(
2626
kill(job.pid, nix::sys::signal::SIGCONT)
2727
.map_err(|err| format!("fg: failed to resume {arg} ({err})"))?;
2828
}
29-
opened_files.write_out(&format!("{}\n", job.command));
29+
opened_files.write_out(format!("{}\n", job.command));
3030
let mut temp = job.command.clone();
3131
std::mem::swap(&mut shell.last_pipeline_command, &mut temp);
3232
shell
@@ -63,7 +63,7 @@ impl BuiltinUtility for Fg {
6363
status = 1;
6464
}
6565
} else {
66-
opened_files.write_err("bg: no background jobs".to_string());
66+
opened_files.write_err("bg: no background jobs");
6767
status = 1;
6868
}
6969
} else {

0 commit comments

Comments
 (0)