mxsm / mxsm/rocketmq-rust

[Refactor♻️] Remove the four redundant unwraps from the getAcl table renderer

Open Beginner friendly
#10,769 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Difficulty level/Easy good first issue help wanted refactor♻️ rocketmq-tools crate rust
Dominant language
Rust
Stars
1.5k
Forks
264
Avg merge
1h 34m
Merged PRs (30d)
567

Description

Target Code
Crate: rocketmq-admin-cli
File: rocketmq-tools/rocketmq-admin/rocketmq-admin-cli/src/commands/auth/get_acl_sub_command.rs
Symbol: print_acl, declared at line 91
Locations: lines 95, 103, 107 and 111
Problems

print_acl checks that an Option is populated and then immediately unwraps it.
Each Option is unwrapped twice, and each unwrap() is guarded by a check on the
line above that the borrow checker does not carry forward:

// line 95 and line 103
    let policies = acl.policies.as_ref();

    if policies.is_none() || policies.unwrap().is_empty() {
        // ... prints an empty row and returns ...
    }

    for policy in policies.unwrap() {
// line 107 and line 111
        let entries = policy.entries.as_ref();

        if entries.is_none() || entries.unwrap().is_empty() {
            continue;
        }

        for entry in entries.unwrap() {

That is four unwrap() calls on a non-test path for a function that already holds
the Option. The condition is also harder to read than it needs to be: a reader
has to work out that is_none() || unwrap().is_empty() means "absent or empty",
and the unwrap() will panic rather than the branch being taken if the guard is
ever edited.

Proposed Changes

Bind the value once with let ... else, which is available because this crate is
on Rust edition 2024, and drop both the is_none() check and the unwrap():

fn print_acl(acl: &AclInfo) {
    let subject = acl.subject.as_ref().map(|s| s.as_str()).unwrap_or("*");

    let Some(policies) = acl.policies.as_ref().filter(|policies| !policies.is_empty()) else {
        println!(
            "{:<24}  {:<12}  {:<24}  {:<20}  {:<24}  {:<12}",
            subject, "", "", "", "", ""
        );
        return;
    };

    for policy in policies {
        let policy_type = policy.policy_type.as_ref().map(|p| p.as_str()).unwrap_or("");
        let Some(entries) = policy.entries.as_ref().filter(|entries| !entries.is_empty()) else {
            continue;
        };

        for entry in entries {
            // unchanged
        }
    }
}

Keep everything else exactly as it is: the printed column widths and header, the
empty-row output, the continue for a policy with no entries, and the
unwrap_or("") fallbacks inside the loop. Change no signature and no output.

If you prefer a narrower change, replacing each entries.unwrap() with a
let ... else and leaving the surrounding structure in place is fine as long as
all four unwrap() calls on these two Options are gone.

Acceptance Criteria

  • Lines 95, 103, 107 and 111 no longer contain unwrap().
  • print_acl no longer contains any unwrap() on policies or entries.
  • An ACL with no policies still prints the single empty row, and a policy with
    no entries is still skipped.
  • The rendered columns, widths, and header are byte for byte unchanged.
  • No public signature changes.

Validation

Run from the repository root:

cargo fmt -p rocketmq-admin-cli -- --check
cargo test -p rocketmq-admin-cli get_acl
cargo clippy -p rocketmq-admin-cli --no-deps -- -D warnings

cargo clippy is worth running here because it is the tool that would flag an
unwrap() left behind by mistake.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at print_acl in rocketmq-tools/rocketmq-admin/rocketmq-admin-cli/src/commands/auth/get_acl_sub_command.rs, focusing on the four listed unwrap locations. Run cargo fmt -p rocketmq-admin-cli -- --check, cargo test -p rocketmq-admin-cli get_acl, and cargo clippy -p rocketmq-admin-cli --no-deps -- -D warnings. Done means the redundant unwraps are removed while empty-row output, skipped policies, and rendered columns remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
90/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.