Skip to content

Commit

Permalink
Rollup merge of #127287 - aDotInTheVoid:jsondocck-index, r=GuillaumeG…
Browse files Browse the repository at this point in the history
…omez

jsondocck: Use correct index for error message.

If you misused a count command like ``@count` $some.selector '"T'"`, you would panic with OOB:

```
thread 'main' panicked at src/tools/jsondocck/src/main.rs:76:92:
index out of bounds: the len is 2 but the index is 2
```

This is because 57c85bd removed the file param, but didn't update the error case. We now error with:

```
Invalid command: Second argument to `@count` must be a valid usize (got `"T"`) on line 20
```

As some point I want to rewrite this code to avoid indexing in general, but this is a nice small fix.

r? `@GuillaumeGomez`
  • Loading branch information
jhpratt committed Jul 4, 2024
2 parents 5712539 + ccc8baf commit 726b9b7
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/tools/jsondocck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub enum CommandKind {

impl CommandKind {
fn validate(&self, args: &[String], lineno: usize) -> bool {
// FIXME(adotinthevoid): We should "parse, don't validate" here, so we avoid ad-hoc
// indexing in check_command.
let count = match self {
CommandKind::Has => (1..=2).contains(&args.len()),
CommandKind::IsMany => args.len() >= 2,
Expand All @@ -71,7 +73,7 @@ impl CommandKind {
if let CommandKind::Count = self {
if args[1].parse::<usize>().is_err() {
print_err(
&format!("Second argument to @count must be a valid usize (got `{}`)", args[2]),
&format!("Second argument to @count must be a valid usize (got `{}`)", args[1]),
lineno,
);
return false;
Expand Down

0 comments on commit 726b9b7

Please sign in to comment.