Skip to content

Commit

Permalink
Merge pull request #5603 from epage/lint-1.80
Browse files Browse the repository at this point in the history
style: Make clippy happy
  • Loading branch information
epage authored Jul 26, 2024
2 parents e014ea6 + 9c6ef3e commit 8a92f42
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 51 deletions.
2 changes: 1 addition & 1 deletion clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ impl Command {
/// [`env::args_os`]: std::env::args_os()
/// [`Command::get_matches`]: Command::get_matches()
pub fn get_matches_mut(&mut self) -> ArgMatches {
self.try_get_matches_from_mut(&mut env::args_os())
self.try_get_matches_from_mut(env::args_os())
.unwrap_or_else(|e| e.exit())
}

Expand Down
2 changes: 1 addition & 1 deletion clap_builder/src/builder/debug_asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub(crate) fn assert_app(cmd: &Command) {
"Command {}: {}",
cmd.get_name(),
"`{bin}` template variable was removed in clap5, use `{name}` instead"
)
);
}

cmd._panic_on_missing_help(cmd.is_help_expected_set());
Expand Down
36 changes: 18 additions & 18 deletions clap_complete/src/dynamic/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn complete(
if value.is_some() {
ParseState::ValueDone
} else {
ParseState::Opt(opt.unwrap().clone())
ParseState::Opt(opt.unwrap())
}
}
Some(clap::ArgAction::SetTrue) | Some(clap::ArgAction::SetFalse) => {
Expand All @@ -115,7 +115,7 @@ pub fn complete(
Some(opt) => {
state = match short.next_value_os() {
Some(_) => ParseState::ValueDone,
None => ParseState::Opt(opt.clone()),
None => ParseState::Opt(opt),
};
}
None => {
Expand All @@ -142,23 +142,23 @@ pub fn complete(
}

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

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

/// Parsing a optional flag argument
Opt(clap::Arg),
Opt(&'a clap::Arg),
}

fn complete_arg(
arg: &clap_lex::ParsedArg<'_>,
cmd: &clap::Command,
current_dir: Option<&std::path::Path>,
pos_index: usize,
state: ParseState,
state: ParseState<'_>,
) -> Result<Vec<CompletionCandidate>, std::io::Error> {
debug!(
"complete_arg: arg={:?}, cmd={:?}, current_dir={:?}, pos_index={:?}, state={:?}",
Expand Down Expand Up @@ -202,7 +202,7 @@ fn complete_arg(
completions.extend(hidden_longs_aliases(cmd).into_iter().filter(|comp| {
comp.get_content()
.starts_with(format!("--{}", flag).as_str())
}))
}));
}
}
} else if arg.is_escape() || arg.is_stdio() || arg.is_empty() {
Expand Down Expand Up @@ -236,7 +236,7 @@ fn complete_arg(
} else if let Some(short) = arg.to_short() {
if !short.is_negative_number() {
// Find the first takes_value option.
let (leading_flags, takes_value_opt, mut short) = parse_shortflags(&cmd, short);
let (leading_flags, takes_value_opt, mut short) = parse_shortflags(cmd, short);

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

let value = short.next_value_os().unwrap_or(OsStr::new(""));
completions.extend(
complete_arg_value(value.to_str().ok_or(value), &opt, current_dir)
complete_arg_value(value.to_str().ok_or(value), opt, current_dir)
.into_iter()
.map(|comp| {
CompletionCandidate::new(format!(
Expand Down Expand Up @@ -299,11 +299,11 @@ fn complete_arg(
}
}
ParseState::Opt(opt) => {
completions.extend(complete_arg_value(arg.to_value(), &opt, current_dir));
completions.extend(complete_arg_value(arg.to_value(), opt, current_dir));
}
}
if completions.iter().any(|a| a.is_visible()) {
completions.retain(|a| a.is_visible())
completions.retain(|a| a.is_visible());
}

Ok(completions)
Expand Down Expand Up @@ -452,7 +452,7 @@ fn longs_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
.filter_map(|a| {
a.get_long_and_visible_aliases().map(|longs| {
longs.into_iter().map(|s| {
CompletionCandidate::new(format!("--{}", s.to_string()))
CompletionCandidate::new(format!("--{}", s))
.help(a.get_help().cloned())
.visible(!a.is_hide_set())
})
Expand All @@ -470,7 +470,7 @@ fn hidden_longs_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
.filter_map(|a| {
a.get_aliases().map(|longs| {
longs.into_iter().map(|s| {
CompletionCandidate::new(format!("--{}", s.to_string()))
CompletionCandidate::new(format!("--{}", s))
.help(a.get_help().cloned())
.visible(false)
})
Expand Down Expand Up @@ -526,7 +526,7 @@ fn subcommands(p: &clap::Command) -> Vec<CompletionCandidate> {
.help(sc.get_about().cloned())
.visible(!sc.is_hide_set())
})
.chain(sc.get_aliases().into_iter().map(|s| {
.chain(sc.get_aliases().map(|s| {
CompletionCandidate::new(s.to_string())
.help(sc.get_about().cloned())
.visible(false)
Expand All @@ -535,11 +535,11 @@ fn subcommands(p: &clap::Command) -> Vec<CompletionCandidate> {
.collect()
}

/// Parse the short flags and find the first takes_value option.
fn parse_shortflags<'s>(
cmd: &clap::Command,
/// Parse the short flags and find the first `takes_value` option.
fn parse_shortflags<'c, 's>(
cmd: &'c clap::Command,
mut short: clap_lex::ShortFlags<'s>,
) -> (OsString, Option<clap::Arg>, clap_lex::ShortFlags<'s>) {
) -> (OsString, Option<&'c clap::Arg>, clap_lex::ShortFlags<'s>) {
let takes_value_opt;
let mut leading_flags = OsString::new();
// Find the first takes_value option.
Expand Down Expand Up @@ -579,7 +579,7 @@ fn parse_shortflags<'s>(
}
}

(leading_flags, takes_value_opt.cloned(), short)
(leading_flags, takes_value_opt, short)
}

/// A completion candidate definition
Expand Down
3 changes: 2 additions & 1 deletion clap_complete/src/dynamic/shells/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ impl crate::dynamic::Completer for Bash {
let mut upper_name = escaped_name.clone();
upper_name.make_ascii_uppercase();

let completer = shlex::quote(completer);
let completer =
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));

let script = r#"
_clap_complete_NAME() {
Expand Down
7 changes: 4 additions & 3 deletions clap_complete/src/dynamic/shells/elvish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ impl crate::dynamic::Completer for Elvish {
}
fn write_registration(
&self,
name: &str,
_name: &str,
bin: &str,
completer: &str,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error> {
let bin = shlex::quote(bin);
let completer = shlex::quote(completer);
let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin));
let completer =
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));

let script = r#"
set edit:completion:arg-completer[BIN] = { |@words|
Expand Down
6 changes: 4 additions & 2 deletions clap_complete/src/dynamic/shells/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ impl crate::dynamic::Completer for Fish {
completer: &str,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error> {
let bin = shlex::quote(bin);
let completer = shlex::quote(completer);
let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin));
let completer =
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));

writeln!(
buf,
r#"complete -x -c {bin} -a "("'{completer}'" complete --shell fish -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))""#
Expand Down
38 changes: 19 additions & 19 deletions clap_complete/src/dynamic/shells/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use crate::dynamic::Completer as _;
/// #[derive(Parser, Debug)]
/// #[clap(name = "dynamic", about = "A dynamic command line tool")]
/// struct Cli {
/// /// The subcommand to run complete
/// #[command(subcommand)]
/// /// The subcommand to run complete
/// #[command(subcommand)]
/// complete: Option<CompleteCommand>,
/// /// Input file path
/// #[clap(short, long, value_hint = clap::ValueHint::FilePath)]
Expand All @@ -54,9 +54,9 @@ use crate::dynamic::Completer as _;
/// let cli = Cli::parse();
/// if let Some(completions) = cli.complete {
/// completions.complete(&mut Cli::command());
/// }
/// }
///
/// // normal logic continues...
/// // normal logic continues...
/// }
///```
///
Expand All @@ -68,29 +68,29 @@ use crate::dynamic::Completer as _;
/// please remember to modify the redirection output file in the following command.
///
/// - Bash
/// ```bash
/// echo "source <(your_program complete --shell bash --register -)" >> ~/.bashrc
/// ```
/// ```bash
/// echo "source <(your_program complete --shell bash --register -)" >> ~/.bashrc
/// ```
///
/// - Fish
/// ```fish
/// echo "source (your_program complete --shell fish --register - | psub)" >> ~/.config/fish/config.fish
/// ```
/// ```fish
/// echo "source (your_program complete --shell fish --register - | psub)" >> ~/.config/fish/config.fish
/// ```
///
/// - Zsh
/// ```zsh
/// echo "source <(your_program complete --shell zsh --register -)" >> ~/.zshrc
/// ```
/// ```zsh
/// echo "source <(your_program complete --shell zsh --register -)" >> ~/.zshrc
/// ```
///
/// - Elvish
/// ```elvish
/// echo "eval (your_program complete --shell elvish --register -)" >> ~/.elvish/rc.elv
/// ```
/// ```elvish
/// echo "eval (your_program complete --shell elvish --register -)" >> ~/.elvish/rc.elv
/// ```
///
/// - Powershell
/// ```powershell
/// echo "your_program complete --shell powershell --register - | Invoke-Expression" >> $PROFILE
/// ```
/// ```powershell
/// echo "your_program complete --shell powershell --register - | Invoke-Expression" >> $PROFILE
/// ```
///
#[derive(clap::Subcommand)]
#[allow(missing_docs)]
Expand Down
6 changes: 4 additions & 2 deletions clap_complete/src/dynamic/shells/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ impl crate::dynamic::Completer for Zsh {
completer: &str,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error> {
let bin = shlex::quote(bin);
let completer = shlex::quote(completer);
let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin));
let completer =
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));

let script = r#"#compdef BIN
function _clap_dynamic_completer() {
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
Expand Down
8 changes: 4 additions & 4 deletions clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn suggest_hidden_long_flags() {
assert_data_eq!(
complete!(cmd, "--hello-world-h"),
snapbox::str!["--hello-world-hidden"]
)
);
}

#[test]
Expand Down Expand Up @@ -90,7 +90,7 @@ test_hidden-alias_visible"
assert_data_eq!(
complete!(cmd, "test_hidden-alias_h"),
snapbox::str!["test_hidden-alias_hidden"]
)
);
}

#[test]
Expand Down Expand Up @@ -142,7 +142,7 @@ fn suggest_hidden_possible_value() {
assert_data_eq!(
complete!(cmd, "--test=test-h"),
snapbox::str!["--test=test-hidden\tSay hello to the moon"]
)
);
}

#[test]
Expand Down Expand Up @@ -436,7 +436,7 @@ pos_c"
assert_data_eq!(
complete!(cmd, "-ciF=[TAB]", current_dir = Some(testdir_path)),
snapbox::str![""]
)
);
}

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

0 comments on commit 8a92f42

Please sign in to comment.