diff --git a/clap_builder/src/builder/debug_asserts.rs b/clap_builder/src/builder/debug_asserts.rs index 6a1fc7935ea..001ef8a1201 100644 --- a/clap_builder/src/builder/debug_asserts.rs +++ b/clap_builder/src/builder/debug_asserts.rs @@ -140,6 +140,12 @@ pub(crate) fn assert_app(cmd: &Command) { // requires, r_if, r_unless for (_predicate, req_id) in &arg.requires { + assert!( + &arg.id != req_id, + "Argument {} cannot require itself", + arg.get_id() + ); + assert!( cmd.id_exists(req_id), "Command {}: Argument or group '{}' specified in 'requires*' for '{}' does not exist", diff --git a/tests/builder/require.rs b/tests/builder/require.rs index d4aa2ac8396..14592ad0a9e 100644 --- a/tests/builder/require.rs +++ b/tests/builder/require.rs @@ -1480,10 +1480,9 @@ For more information, try '--help'. } #[test] -/// This test demonstrates existing broken behavior, ideally it should panic +#[should_panic = "Argument flag cannot require itself"] fn requires_self() { - let result = Command::new("flag_required") + let _result = Command::new("flag_required") .arg(arg!(-f --flag "some flag").requires("flag")) .try_get_matches_from(vec![""]); - assert!(result.is_ok()); }