Skip to content

Commit 18a81c4

Browse files
authored
Merge pull request #5836 from omertuc/reqself
fix(assert): Prevent arguments from requiring self
2 parents b4ea2d4 + 29d9e88 commit 18a81c4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

clap_builder/src/builder/debug_asserts.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,18 @@ pub(crate) fn assert_app(cmd: &Command) {
139139
}
140140

141141
// requires, r_if, r_unless
142-
for req in &arg.requires {
142+
for (_predicate, req_id) in &arg.requires {
143143
assert!(
144-
cmd.id_exists(&req.1),
144+
&arg.id != req_id,
145+
"Argument {} cannot require itself",
146+
arg.get_id()
147+
);
148+
149+
assert!(
150+
cmd.id_exists(req_id),
145151
"Command {}: Argument or group '{}' specified in 'requires*' for '{}' does not exist",
146152
cmd.get_name(),
147-
req.1,
153+
req_id,
148154
arg.get_id(),
149155
);
150156
}

tests/builder/require.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1478,3 +1478,11 @@ For more information, try '--help'.
14781478
";
14791479
utils::assert_output(cmd, "test --require-first --second", EXPECTED, true);
14801480
}
1481+
1482+
#[test]
1483+
#[should_panic = "Argument flag cannot require itself"]
1484+
fn requires_self() {
1485+
let _result = Command::new("flag_required")
1486+
.arg(arg!(-f --flag "some flag").requires("flag"))
1487+
.try_get_matches_from(vec![""]);
1488+
}

0 commit comments

Comments
 (0)