Skip to content

Commit 29d9e88

Browse files
committed
fix(assert): Prevent arguments from requiring self
It's non-sensical for an argument to require itself, so it must be a mistake, and should be prevented. This is arguably a breaking change, but of the spacebar heating kind. Signed-off-by: Omer Tuchfeld <[email protected]>
1 parent 52aad0e commit 29d9e88

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

clap_builder/src/builder/debug_asserts.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ pub(crate) fn assert_app(cmd: &Command) {
140140

141141
// requires, r_if, r_unless
142142
for (_predicate, req_id) in &arg.requires {
143+
assert!(
144+
&arg.id != req_id,
145+
"Argument {} cannot require itself",
146+
arg.get_id()
147+
);
148+
143149
assert!(
144150
cmd.id_exists(req_id),
145151
"Command {}: Argument or group '{}' specified in 'requires*' for '{}' does not exist",

tests/builder/require.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,10 +1480,9 @@ For more information, try '--help'.
14801480
}
14811481

14821482
#[test]
1483-
/// This test demonstrates existing broken behavior, ideally it should panic
1483+
#[should_panic = "Argument flag cannot require itself"]
14841484
fn requires_self() {
1485-
let result = Command::new("flag_required")
1485+
let _result = Command::new("flag_required")
14861486
.arg(arg!(-f --flag "some flag").requires("flag"))
14871487
.try_get_matches_from(vec![""]);
1488-
assert!(result.is_ok());
14891488
}

0 commit comments

Comments
 (0)