Aiclean

From radareorg/radare2-skills · View interactive version

Aiclean is an AI agent skill from the GitHub repository radareorg/radare2-skills. Clean and refactor code with a focus on unnecessary complexity, reducing lines of code, improving readability, and preferring radare2-native portable APIs.

How to use this skill

Skill file

When this skill is used:

Read the AGENTS.md for project guidelines first.

Workflow:

  1. Check the whole change: the branch diff against its merge-base plus any uncommitted work, and the functions or URLs given as context.

Code already committed on the branch is still unreviewed; a scope given by the invoker narrows within that diff, never below it.

  1. Analyze the files involved and take a list of common coding practices from the project.
  2. Inspect recent cleanup-oriented history in the same subsystem before patching.

Search for commits using terms like clean, cleanup, refactor, simplify, dedup, portable, dead code, reuse.

  1. Search for an existing API before polishing any new walk, parse, or traversal.

Grep libr/include/ for _foreach, _recurse, _dfs, _iter and check the cmd_*.inc.c help tables; deleting new code in favor of an existing API is the best cleanup.

Grep the patched file too, for how it already expresses the same thing; matching the local idiom is usually cheaper than any new helper.

  1. Create a small plan that favors behavior-preserving simplification, LOC reduction, and safer ownership/bounds handling.

Sweep every new or changed construct (each struct, loop, string test, arithmetic expression, null check, and duplicated block) against the rules below rather than only the obvious spots — maintainer review nitpicks tend to hit the constructs a quick pass skips.

Apply a fix that is about a concept at every call site of that concept, not only at the one that surfaced it.

  1. Validate with focused tests or builds for the touched area when feasible.

Keep one case where the change must be inert (the other config value, the input outside the predicate) and check it is green on base and branch; two cases that fail for the same mutation are one case.

Prove each behavior in the diff is tested: break it (invert the condition, drop the write, return early), name the test that fails, then restore the code before the next one; a behavior no test catches is untested however many tests pass.

Check the mutation hits that behavior and nothing else, or its result means nothing.

Write new tests against a synthetic fixture (malloc:// plus wx) rather than a real binary plus full analysis, whose output differs between hosts, and assert semantic fields instead of absolute character offsets, which move with unrelated strings.

Then improve the code quality following these rules:

Prefer bool a = true; instead of bool a; a = true;

Prefer patterns like RPanelPos *pos = &p->view->pos; or const ut64 bsz = core->blocksize;

Use r_strbuf_* or r_str_newf instead of repeated append chains or sprintf/strcat

Use r_read_le* and r_read_be* instead of open-coded byte parsing

Parse user-supplied numbers with r_num_get/r_num_math (command handlers have core->num) instead of atoi/strtol/sscanf, so 0x forms, negative values, flags and expressions all resolve; atoi on a value printed in hex silently yields the wrong number

When "not a number" has to be told apart from a valid 0, use r_num_get_err/r_num_math_err and test the err out-param, or r_num_failed (core->num) — the plain forms return 0 for garbage, so a bare replacement can turn a typo into a silent hit on whatever lives at offset 0

Use r_mem_dup, r_list_purge, R_NEWS0, vector/list helpers, RTable, Sdb, and other existing r2 primitives before inventing new helpers

Use the r_str.h char-scan family (r_str_rchr, r_str_lchr, r_str_nchr, r_sub_str_rchr) over strchr/strrchr chains; r_str_rchr takes a start position, so a second backward search resumes from the previous match instead of rescanning

Avoid sprintf, strcpy, strcat, open-coded endian reads, UB-prone casts, and non-portable format strings

Use <r_types.h> types and PFMT64 macros

Patterns repeatedly seen in radare2 cleanup history:

r_anal_block_recurse_depth_first already yields post-order via on_exit

Back-edge and dominator analysis already ship too: r_anal_function_get_graph, r_graph_dfs_node with RGraphVisitor.back_edge, and r_graph_dom_tree

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.