Skip to content

Commit 8a92f42

Browse files
authored
Merge pull request #5603 from epage/lint-1.80
style: Make clippy happy
2 parents e014ea6 + 9c6ef3e commit 8a92f42

File tree

9 files changed

+57
-51
lines changed

9 files changed

+57
-51
lines changed

clap_builder/src/builder/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl Command {
631631
/// [`env::args_os`]: std::env::args_os()
632632
/// [`Command::get_matches`]: Command::get_matches()
633633
pub fn get_matches_mut(&mut self) -> ArgMatches {
634-
self.try_get_matches_from_mut(&mut env::args_os())
634+
self.try_get_matches_from_mut(env::args_os())
635635
.unwrap_or_else(|e| e.exit())
636636
}
637637

clap_builder/src/builder/debug_asserts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub(crate) fn assert_app(cmd: &Command) {
370370
"Command {}: {}",
371371
cmd.get_name(),
372372
"`{bin}` template variable was removed in clap5, use `{name}` instead"
373-
)
373+
);
374374
}
375375

376376
cmd._panic_on_missing_help(cmd.is_help_expected_set());

clap_complete/src/dynamic/completer.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn complete(
9292
if value.is_some() {
9393
ParseState::ValueDone
9494
} else {
95-
ParseState::Opt(opt.unwrap().clone())
95+
ParseState::Opt(opt.unwrap())
9696
}
9797
}
9898
Some(clap::ArgAction::SetTrue) | Some(clap::ArgAction::SetFalse) => {
@@ -115,7 +115,7 @@ pub fn complete(
115115
Some(opt) => {
116116
state = match short.next_value_os() {
117117
Some(_) => ParseState::ValueDone,
118-
None => ParseState::Opt(opt.clone()),
118+
None => ParseState::Opt(opt),
119119
};
120120
}
121121
None => {
@@ -142,23 +142,23 @@ pub fn complete(
142142
}
143143

144144
#[derive(Debug, PartialEq, Eq, Clone)]
145-
enum ParseState {
145+
enum ParseState<'a> {
146146
/// Parsing a value done, there is no state to record.
147147
ValueDone,
148148

149149
/// Parsing a positional argument after `--`
150150
Pos(usize),
151151

152152
/// Parsing a optional flag argument
153-
Opt(clap::Arg),
153+
Opt(&'a clap::Arg),
154154
}
155155

156156
fn complete_arg(
157157
arg: &clap_lex::ParsedArg<'_>,
158158
cmd: &clap::Command,
159159
current_dir: Option<&std::path::Path>,
160160
pos_index: usize,
161-
state: ParseState,
161+
state: ParseState<'_>,
162162
) -> Result<Vec<CompletionCandidate>, std::io::Error> {
163163
debug!(
164164
"complete_arg: arg={:?}, cmd={:?}, current_dir={:?}, pos_index={:?}, state={:?}",
@@ -202,7 +202,7 @@ fn complete_arg(
202202
completions.extend(hidden_longs_aliases(cmd).into_iter().filter(|comp| {
203203
comp.get_content()
204204
.starts_with(format!("--{}", flag).as_str())
205-
}))
205+
}));
206206
}
207207
}
208208
} else if arg.is_escape() || arg.is_stdio() || arg.is_empty() {
@@ -236,7 +236,7 @@ fn complete_arg(
236236
} else if let Some(short) = arg.to_short() {
237237
if !short.is_negative_number() {
238238
// Find the first takes_value option.
239-
let (leading_flags, takes_value_opt, mut short) = parse_shortflags(&cmd, short);
239+
let (leading_flags, takes_value_opt, mut short) = parse_shortflags(cmd, short);
240240

241241
// Clone `short` to `peek_short` to peek whether the next flag is a `=`.
242242
if let Some(opt) = takes_value_opt {
@@ -250,7 +250,7 @@ fn complete_arg(
250250

251251
let value = short.next_value_os().unwrap_or(OsStr::new(""));
252252
completions.extend(
253-
complete_arg_value(value.to_str().ok_or(value), &opt, current_dir)
253+
complete_arg_value(value.to_str().ok_or(value), opt, current_dir)
254254
.into_iter()
255255
.map(|comp| {
256256
CompletionCandidate::new(format!(
@@ -299,11 +299,11 @@ fn complete_arg(
299299
}
300300
}
301301
ParseState::Opt(opt) => {
302-
completions.extend(complete_arg_value(arg.to_value(), &opt, current_dir));
302+
completions.extend(complete_arg_value(arg.to_value(), opt, current_dir));
303303
}
304304
}
305305
if completions.iter().any(|a| a.is_visible()) {
306-
completions.retain(|a| a.is_visible())
306+
completions.retain(|a| a.is_visible());
307307
}
308308

309309
Ok(completions)
@@ -452,7 +452,7 @@ fn longs_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
452452
.filter_map(|a| {
453453
a.get_long_and_visible_aliases().map(|longs| {
454454
longs.into_iter().map(|s| {
455-
CompletionCandidate::new(format!("--{}", s.to_string()))
455+
CompletionCandidate::new(format!("--{}", s))
456456
.help(a.get_help().cloned())
457457
.visible(!a.is_hide_set())
458458
})
@@ -470,7 +470,7 @@ fn hidden_longs_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
470470
.filter_map(|a| {
471471
a.get_aliases().map(|longs| {
472472
longs.into_iter().map(|s| {
473-
CompletionCandidate::new(format!("--{}", s.to_string()))
473+
CompletionCandidate::new(format!("--{}", s))
474474
.help(a.get_help().cloned())
475475
.visible(false)
476476
})
@@ -526,7 +526,7 @@ fn subcommands(p: &clap::Command) -> Vec<CompletionCandidate> {
526526
.help(sc.get_about().cloned())
527527
.visible(!sc.is_hide_set())
528528
})
529-
.chain(sc.get_aliases().into_iter().map(|s| {
529+
.chain(sc.get_aliases().map(|s| {
530530
CompletionCandidate::new(s.to_string())
531531
.help(sc.get_about().cloned())
532532
.visible(false)
@@ -535,11 +535,11 @@ fn subcommands(p: &clap::Command) -> Vec<CompletionCandidate> {
535535
.collect()
536536
}
537537

538-
/// Parse the short flags and find the first takes_value option.
539-
fn parse_shortflags<'s>(
540-
cmd: &clap::Command,
538+
/// Parse the short flags and find the first `takes_value` option.
539+
fn parse_shortflags<'c, 's>(
540+
cmd: &'c clap::Command,
541541
mut short: clap_lex::ShortFlags<'s>,
542-
) -> (OsString, Option<clap::Arg>, clap_lex::ShortFlags<'s>) {
542+
) -> (OsString, Option<&'c clap::Arg>, clap_lex::ShortFlags<'s>) {
543543
let takes_value_opt;
544544
let mut leading_flags = OsString::new();
545545
// Find the first takes_value option.
@@ -579,7 +579,7 @@ fn parse_shortflags<'s>(
579579
}
580580
}
581581

582-
(leading_flags, takes_value_opt.cloned(), short)
582+
(leading_flags, takes_value_opt, short)
583583
}
584584

585585
/// A completion candidate definition

clap_complete/src/dynamic/shells/bash.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ impl crate::dynamic::Completer for Bash {
2323
let mut upper_name = escaped_name.clone();
2424
upper_name.make_ascii_uppercase();
2525

26-
let completer = shlex::quote(completer);
26+
let completer =
27+
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));
2728

2829
let script = r#"
2930
_clap_complete_NAME() {

clap_complete/src/dynamic/shells/elvish.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ impl crate::dynamic::Completer for Elvish {
88
}
99
fn write_registration(
1010
&self,
11-
name: &str,
11+
_name: &str,
1212
bin: &str,
1313
completer: &str,
1414
buf: &mut dyn std::io::Write,
1515
) -> Result<(), std::io::Error> {
16-
let bin = shlex::quote(bin);
17-
let completer = shlex::quote(completer);
16+
let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin));
17+
let completer =
18+
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));
1819

1920
let script = r#"
2021
set edit:completion:arg-completer[BIN] = { |@words|

clap_complete/src/dynamic/shells/fish.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ impl crate::dynamic::Completer for Fish {
1313
completer: &str,
1414
buf: &mut dyn std::io::Write,
1515
) -> Result<(), std::io::Error> {
16-
let bin = shlex::quote(bin);
17-
let completer = shlex::quote(completer);
16+
let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin));
17+
let completer =
18+
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));
19+
1820
writeln!(
1921
buf,
2022
r#"complete -x -c {bin} -a "("'{completer}'" complete --shell fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))""#

clap_complete/src/dynamic/shells/mod.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use crate::dynamic::Completer as _;
3939
/// #[derive(Parser, Debug)]
4040
/// #[clap(name = "dynamic", about = "A dynamic command line tool")]
4141
/// struct Cli {
42-
/// /// The subcommand to run complete
43-
/// #[command(subcommand)]
42+
/// /// The subcommand to run complete
43+
/// #[command(subcommand)]
4444
/// complete: Option<CompleteCommand>,
4545
/// /// Input file path
4646
/// #[clap(short, long, value_hint = clap::ValueHint::FilePath)]
@@ -54,9 +54,9 @@ use crate::dynamic::Completer as _;
5454
/// let cli = Cli::parse();
5555
/// if let Some(completions) = cli.complete {
5656
/// completions.complete(&mut Cli::command());
57-
/// }
57+
/// }
5858
///
59-
/// // normal logic continues...
59+
/// // normal logic continues...
6060
/// }
6161
///```
6262
///
@@ -68,29 +68,29 @@ use crate::dynamic::Completer as _;
6868
/// please remember to modify the redirection output file in the following command.
6969
///
7070
/// - Bash
71-
/// ```bash
72-
/// echo "source <(your_program complete --shell bash --register -)" >> ~/.bashrc
73-
/// ```
71+
/// ```bash
72+
/// echo "source <(your_program complete --shell bash --register -)" >> ~/.bashrc
73+
/// ```
7474
///
7575
/// - Fish
76-
/// ```fish
77-
/// echo "source (your_program complete --shell fish --register - | psub)" >> ~/.config/fish/config.fish
78-
/// ```
76+
/// ```fish
77+
/// echo "source (your_program complete --shell fish --register - | psub)" >> ~/.config/fish/config.fish
78+
/// ```
7979
///
8080
/// - Zsh
81-
/// ```zsh
82-
/// echo "source <(your_program complete --shell zsh --register -)" >> ~/.zshrc
83-
/// ```
81+
/// ```zsh
82+
/// echo "source <(your_program complete --shell zsh --register -)" >> ~/.zshrc
83+
/// ```
8484
///
8585
/// - Elvish
86-
/// ```elvish
87-
/// echo "eval (your_program complete --shell elvish --register -)" >> ~/.elvish/rc.elv
88-
/// ```
86+
/// ```elvish
87+
/// echo "eval (your_program complete --shell elvish --register -)" >> ~/.elvish/rc.elv
88+
/// ```
8989
///
9090
/// - Powershell
91-
/// ```powershell
92-
/// echo "your_program complete --shell powershell --register - | Invoke-Expression" >> $PROFILE
93-
/// ```
91+
/// ```powershell
92+
/// echo "your_program complete --shell powershell --register - | Invoke-Expression" >> $PROFILE
93+
/// ```
9494
///
9595
#[derive(clap::Subcommand)]
9696
#[allow(missing_docs)]

clap_complete/src/dynamic/shells/zsh.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ impl crate::dynamic::Completer for Zsh {
1313
completer: &str,
1414
buf: &mut dyn std::io::Write,
1515
) -> Result<(), std::io::Error> {
16-
let bin = shlex::quote(bin);
17-
let completer = shlex::quote(completer);
16+
let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin));
17+
let completer =
18+
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));
19+
1820
let script = r#"#compdef BIN
1921
function _clap_dynamic_completer() {
2022
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)

clap_complete/tests/testsuite/dynamic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn suggest_hidden_long_flags() {
5252
assert_data_eq!(
5353
complete!(cmd, "--hello-world-h"),
5454
snapbox::str!["--hello-world-hidden"]
55-
)
55+
);
5656
}
5757

5858
#[test]
@@ -90,7 +90,7 @@ test_hidden-alias_visible"
9090
assert_data_eq!(
9191
complete!(cmd, "test_hidden-alias_h"),
9292
snapbox::str!["test_hidden-alias_hidden"]
93-
)
93+
);
9494
}
9595

9696
#[test]
@@ -142,7 +142,7 @@ fn suggest_hidden_possible_value() {
142142
assert_data_eq!(
143143
complete!(cmd, "--test=test-h"),
144144
snapbox::str!["--test=test-hidden\tSay hello to the moon"]
145-
)
145+
);
146146
}
147147

148148
#[test]
@@ -436,7 +436,7 @@ pos_c"
436436
assert_data_eq!(
437437
complete!(cmd, "-ciF=[TAB]", current_dir = Some(testdir_path)),
438438
snapbox::str![""]
439-
)
439+
);
440440
}
441441

442442
fn complete(cmd: &mut Command, args: impl AsRef<str>, current_dir: Option<&Path>) -> String {

0 commit comments

Comments
 (0)