Skip to content

Commit

Permalink
chore: Address pedantic clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
jpgrayson committed Nov 8, 2022
1 parent eb8a86d commit c99039f
Show file tree
Hide file tree
Showing 38 changed files with 219 additions and 205 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() {
.ok();
let git_hash = git_output
.as_ref()
.and_then(|output| std::str::from_utf8(&output.stdout).ok().map(|s| s.trim()))
.and_then(|output| std::str::from_utf8(&output.stdout).ok().map(str::trim))
// E.g. "v2.0.0-beta.2-7-g12ab34cd56*"
// ttttttttttttt N hhhhhhhhhhhD
// t => last matching annotated tag
Expand Down
2 changes: 1 addition & 1 deletion src/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn split_command_line(line: &str) -> Result<Vec<String>, String> {
argv.push(completed_word);
skip_spaces = true;
} else {
word.push(c)
word.push(c);
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/argset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub(crate) fn parse_branch_name(name: &str) -> anyhow::Result<String> {
/// since that function returns `Option<&String>` which often needs to be mapped to
/// `Option<&str>`.
pub(crate) fn get_one_str<'a>(matches: &'a clap::ArgMatches, id: &str) -> Option<&'a str> {
matches.get_one::<String>(id).map(|s| s.as_str())
matches.get_one::<String>(id).map(String::as_str)
}

/// For use with `clap::Arg::value_parser()` to parse a usize argument.
Expand Down Expand Up @@ -103,7 +103,7 @@ pub(crate) fn get_diff_opts(

if let Ok(value) = config.get_string("stgit.diff-opts") {
for arg in value.split_ascii_whitespace() {
opts.push(String::from(arg))
opts.push(String::from(arg));
}
}

Expand All @@ -112,7 +112,7 @@ pub(crate) fn get_diff_opts(
}

if force_full_index {
opts.push(String::from("--full-index"))
opts.push(String::from("--full-index"));
}

if force_binary {
Expand Down
62 changes: 31 additions & 31 deletions src/cmd/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ fn run(matches: &ArgMatches) -> Result<()> {
let repo = git2::Repository::open_from_env()?;
if let Some((subname, submatches)) = matches.subcommand() {
match subname {
"--list" => list(repo, submatches),
"--create" => create(repo, submatches),
"--clone" => clone(repo, submatches),
"--rename" => rename(repo, submatches),
"--protect" => protect(repo, submatches),
"--unprotect" => unprotect(repo, submatches),
"--delete" => delete(repo, submatches),
"--cleanup" => cleanup(repo, submatches),
"--describe" => describe(repo, submatches),
"--list" => list(&repo, submatches),
"--create" => create(&repo, submatches),
"--clone" => clone(&repo, submatches),
"--rename" => rename(&repo, submatches),
"--protect" => protect(&repo, submatches),
"--unprotect" => unprotect(&repo, submatches),
"--delete" => delete(&repo, submatches),
"--cleanup" => cleanup(&repo, submatches),
"--describe" => describe(&repo, submatches),
s => panic!("unhandled branch subcommand {s}"),
}
} else {
Expand Down Expand Up @@ -316,7 +316,7 @@ fn set_stgit_parent(
}
}

fn list(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
fn list(repo: &git2::Repository, matches: &ArgMatches) -> Result<()> {
let mut branchnames = Vec::new();
for branch_result in repo.branches(Some(git2::BranchType::Local))? {
let (branch, _branch_type) = branch_result?;
Expand All @@ -328,13 +328,13 @@ fn list(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
}

branchnames.sort();
let branchname_width = branchnames.iter().map(|s| s.len()).max();
let branchname_width = branchnames.iter().map(String::len).max();

let current_branchname = repo.get_branch(None).ok().and_then(|branch| {
branch
.name()
.ok()
.and_then(|name| name.map(|s| s.to_string()))
.and_then(|name| name.map(ToString::to_string))
});

let config = repo.config()?;
Expand All @@ -354,7 +354,7 @@ fn list(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
write!(stdout, " ")?;
};

if let Ok(stack) = Stack::from_branch(&repo, Some(branchname)) {
if let Ok(stack) = Stack::from_branch(repo, Some(branchname)) {
color_spec.set_fg(Some(termcolor::Color::Cyan));
stdout.set_color(&color_spec)?;
write!(stdout, "s")?;
Expand Down Expand Up @@ -401,7 +401,7 @@ fn list(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
Ok(())
}

fn create(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
fn create(repo: &git2::Repository, matches: &ArgMatches) -> Result<()> {
let new_branchname = get_one_str(matches, "new-branch").expect("required argument");

repo.check_repository_state()?;
Expand Down Expand Up @@ -441,7 +441,7 @@ fn create(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
let target_commit = if let Some(parent_branch) = parent_branch.as_ref() {
parent_branch.get().peel_to_commit()?
} else if let Some(committish) = get_one_str(matches, "committish") {
crate::revspec::parse_stgit_revision(&repo, Some(committish), None)?.peel_to_commit()?
crate::revspec::parse_stgit_revision(repo, Some(committish), None)?.peel_to_commit()?
} else {
repo.head()?.peel_to_commit()?
};
Expand All @@ -454,7 +454,7 @@ fn create(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {

let mut config = repo.config()?;
let mut new_branch = repo.branch(new_branchname, &target_commit, false)?;
let stack = match Stack::initialize(&repo, Some(new_branchname)) {
let stack = match Stack::initialize(repo, Some(new_branchname)) {
Ok(stack) => stack,
Err(e) => {
new_branch.delete()?;
Expand Down Expand Up @@ -493,7 +493,7 @@ fn create(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
}
}

fn clone(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
fn clone(repo: &git2::Repository, matches: &ArgMatches) -> Result<()> {
let current_branch = repo.get_branch(None)?;
let current_branchname = get_branch_name(&current_branch)?;

Expand All @@ -510,7 +510,7 @@ fn clone(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
repo.check_repository_state()?;
statuses.check_conflicts()?;

if let Ok(stack) = Stack::from_branch(&repo, None) {
if let Ok(stack) = Stack::from_branch(repo, None) {
stack.check_head_top_mismatch()?;
let state_ref = repo
.find_reference(&stack.refname)
Expand All @@ -525,7 +525,7 @@ fn clone(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
stupid.branch_copy(None, &new_branchname)?;
} else {
stupid.branch_copy(None, &new_branchname)?;
Stack::initialize(&repo, Some(&new_branchname))?;
Stack::initialize(repo, Some(&new_branchname))?;
};

let mut config = repo.config()?;
Expand All @@ -542,11 +542,11 @@ fn clone(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
stupid.checkout(new_branch.name().unwrap().unwrap())
}

fn rename(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
fn rename(repo: &git2::Repository, matches: &ArgMatches) -> Result<()> {
let names: Vec<_> = matches
.get_many::<String>("branch-any")
.unwrap()
.map(|s| s.as_str())
.map(String::as_str)
.collect();
let current_branchname;
let (old_branchname, new_branchname) = if names.len() == 2 {
Expand All @@ -562,7 +562,7 @@ fn rename(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
let mut config = repo.config()?;
let parent_branchname = get_stgit_parent(&config, old_branchname);

if let Ok(stack) = Stack::from_branch(&repo, Some(old_branchname)) {
if let Ok(stack) = Stack::from_branch(repo, Some(old_branchname)) {
let state_commit = repo
.find_reference(&stack.refname)
.expect("just found this stack state reference")
Expand All @@ -582,19 +582,19 @@ fn rename(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
Ok(())
}

fn protect(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
let stack = Stack::from_branch(&repo, get_one_str(matches, "branch"))?;
fn protect(repo: &git2::Repository, matches: &ArgMatches) -> Result<()> {
let stack = Stack::from_branch(repo, get_one_str(matches, "branch"))?;
let mut config = repo.config()?;
stack.set_protected(&mut config, true)
}

fn unprotect(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
let stack = Stack::from_branch(&repo, get_one_str(matches, "branch"))?;
fn unprotect(repo: &git2::Repository, matches: &ArgMatches) -> Result<()> {
let stack = Stack::from_branch(repo, get_one_str(matches, "branch"))?;
let mut config = repo.config()?;
stack.set_protected(&mut config, false)
}

fn delete(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
fn delete(repo: &git2::Repository, matches: &ArgMatches) -> Result<()> {
let target_branchname = get_one_str(matches, "branch-any").expect("required argument");
let mut target_branch = repo.get_branch(Some(target_branchname))?;
let current_branch = repo.get_branch(None).ok();
Expand All @@ -605,7 +605,7 @@ fn delete(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {

let config = repo.config()?;

if let Ok(stack) = Stack::from_branch(&repo, Some(target_branchname)) {
if let Ok(stack) = Stack::from_branch(repo, Some(target_branchname)) {
if stack.is_protected(&config) {
return Err(anyhow!("Delete not permitted: this branch is protected"));
} else if !matches.get_flag("force") && stack.all_patches().count() > 0 {
Expand All @@ -620,8 +620,8 @@ fn delete(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
Ok(())
}

fn cleanup(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
let stack = Stack::from_branch(&repo, get_one_str(matches, "branch"))?;
fn cleanup(repo: &git2::Repository, matches: &ArgMatches) -> Result<()> {
let stack = Stack::from_branch(repo, get_one_str(matches, "branch"))?;
let config = repo.config()?;
if stack.is_protected(&config) {
return Err(anyhow!("Clean up not permitted: this branch is protected"));
Expand All @@ -634,7 +634,7 @@ fn cleanup(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
Ok(())
}

fn describe(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
fn describe(repo: &git2::Repository, matches: &ArgMatches) -> Result<()> {
let branch = repo.get_branch(get_one_str(matches, "branch-any"))?;
let description = get_one_str(matches, "description").expect("required argument");
let branchname = get_branch_name(&branch)?;
Expand Down
91 changes: 59 additions & 32 deletions src/cmd/completion/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(super) fn dispatch(matches: &clap::ArgMatches) -> Result<()> {

script.raw(HEADER);

let mut stg = crate::get_full_command(crate::alias::Aliases::new(), None);
let mut stg = crate::get_full_command(&crate::alias::Aliases::new(), None);
stg.build();

for command in stg.get_subcommands() {
Expand Down Expand Up @@ -210,34 +210,55 @@ fn insert_compreply(script: &mut ShStream, arg: &clap::Arg) {
) {
match arg.get_id().as_str() {
"branch" | "ref-branch" => {
script.line("mapfile -t COMPREPLY < <(compgen -W \"$(_stg_branches)\" -- \"$cur\")")
script
.line("mapfile -t COMPREPLY < <(compgen -W \"$(_stg_branches)\" -- \"$cur\")");
}

"branch-any" => {
script.line("mapfile -t COMPREPLY < <(compgen -W \"$(_all_branches)\" -- \"$cur\")")
script
.line("mapfile -t COMPREPLY < <(compgen -W \"$(_all_branches)\" -- \"$cur\")");
}
"committish" => script.line(
"committish" => {
script.line(
"mapfile -t COMPREPLY < <(compgen -W \"$(_all_branches) $(_tags) $(_remotes)\")",
),
"git-diff-opt" => script
.line("mapfile -t COMPREPLY < <(compgen -W \"$(_git_diff_opts)\" -- \"$cur\")"),
"git-format-patch-opt" => script.line(
);
}
"git-diff-opt" => {
script
.line("mapfile -t COMPREPLY < <(compgen -W \"$(_git_diff_opts)\" -- \"$cur\")");
}
"git-format-patch-opt" => {
script.line(
"mapfile -t COMPREPLY < <(compgen -W \"$(_git_format_patch_opts)\" -- \"$cur\")",
),
"git-send-email-opt" => script.line(
"mapfile -t COMPREPLY < <(compgen -W \"$(_git_send_email_opts)\" -- \"$cur\")",
),
"patch" => script
.line("mapfile -t COMPREPLY < <(compgen -W \"$(_visible_patches)\" -- \"$cur\")"),
"patchranges" => script.line("_patch_range \"$(_visible_patches)\""),
);
}
"git-send-email-opt" => {
script.line(
"mapfile -t COMPREPLY < <(compgen -W \"$(_git_send_email_opts)\" -- \"$cur\")",
);
}
"patch" => {
script.line(
"mapfile -t COMPREPLY < <(compgen -W \"$(_visible_patches)\" -- \"$cur\")",
);
}
"patchranges" => {
script.line("_patch_range \"$(_visible_patches)\"");
}
"patchranges-all" | "set-tree" | "stgit-revision" => {
script.line("_patch_range \"$(_all_patches)\"")
script.line("_patch_range \"$(_all_patches)\"");
}
"patchranges-applied" => {
script.line("_patch_range \"$(_applied_patches)\"");
}
"patchranges-hidden" => {
script.line("_patch_range \"$(_hidden_patches)\"");
}
"patchranges-unapplied" => {
script.line("_patch_range \"$(_unapplied_patches)\"");
}
"patchranges-applied" => script.line("_patch_range \"$(_applied_patches)\""),
"patchranges-hidden" => script.line("_patch_range \"$(_hidden_patches)\""),
"patchranges-unapplied" => script.line("_patch_range \"$(_unapplied_patches)\""),
"pathspecs" => {
script.line("mapfile -t COMPREPLY < <(compgen -o filenames -A file -- \"$cur\")")
script.line("mapfile -t COMPREPLY < <(compgen -o filenames -A file -- \"$cur\")");
}
"subcommand" => {
// N.B. the `$__subcommands` here refers to a variable in the *calling
Expand All @@ -260,28 +281,35 @@ fn insert_compreply(script: &mut ShStream, arg: &clap::Arg) {
script.dedent();
script.line("fi");
}
_ => script.line(":"),
_ => {
script.line(":");
}
};
} else {
match arg.get_value_hint() {
clap::ValueHint::Unknown | clap::ValueHint::Other => panic!(),
clap::ValueHint::AnyPath => {
script.line("mapfile -t COMPREPLY < <(compgen -o default -- \"$cur\")")
script.line("mapfile -t COMPREPLY < <(compgen -o default -- \"$cur\")");
}
clap::ValueHint::FilePath => {
script.line("mapfile -t COMPREPLY < <(compgen -o filenames -A file -- \"$cur\")")
script.line("mapfile -t COMPREPLY < <(compgen -o filenames -A file -- \"$cur\")");
}
clap::ValueHint::DirPath => {
script.line(
"mapfile -t COMPREPLY < <(compgen -o directory -A directory -- \"$cur\")",
);
}
clap::ValueHint::EmailAddress => {
script.line(":");
}
clap::ValueHint::DirPath => script
.line("mapfile -t COMPREPLY < <(compgen -o directory -A directory -- \"$cur\")"),
clap::ValueHint::EmailAddress => script.line(":"),
clap::ValueHint::CommandName => {
script.line("mapfile -t COMPREPLY < <(compgen -A command -- \"$cur\")")
script.line("mapfile -t COMPREPLY < <(compgen -A command -- \"$cur\")");
}
clap::ValueHint::Username => {
script.line("mapfile -t COMPREPLY < <(compgen -A user -- \"$cur\")")
script.line("mapfile -t COMPREPLY < <(compgen -A user -- \"$cur\")");
}
clap::ValueHint::Hostname => {
script.line("mapfile -t COMPREPLY < <(compgen -A hostname -- \"$cur\")")
script.line("mapfile -t COMPREPLY < <(compgen -A hostname -- \"$cur\")");
}
clap::ValueHint::ExecutablePath => todo!(),
clap::ValueHint::CommandString => todo!(),
Expand All @@ -307,8 +335,7 @@ fn get_flags(command: &clap::Command) -> (ShStream, ShStream) {
for name in longs {
if arg
.get_num_args()
.map(|value_range| value_range.takes_values())
.unwrap_or(false)
.map_or(false, |value_range| value_range.takes_values())
{
long_flags.word(&f!("--{name}="));
} else {
Expand Down Expand Up @@ -341,7 +368,7 @@ fn get_flags(command: &clap::Command) -> (ShStream, ShStream) {
}
}

if command.get_positionals().any(|arg| arg.is_last_set()) {
if command.get_positionals().any(clap::Arg::is_last_set) {
long_flags.word("'-- '");
}

Expand Down
Loading

0 comments on commit c99039f

Please sign in to comment.