about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2024-07-04 04:09:50 -0400
committerGitHub <noreply@github.com>2024-07-04 04:09:50 -0400
commit726b9b7c272debc4426a82b42b0fd594f82c4459 (patch)
tree0e0273133bd9a137dc7c172964fd4c0bf5624b96
parent5712539a627ef2f8bf3ce8889940b3ba4dc55e6f (diff)
parentccc8baf08aa5a204a7df55e67022aa6c5254966c (diff)
downloadrust-726b9b7c272debc4426a82b42b0fd594f82c4459.tar.gz
rust-726b9b7c272debc4426a82b42b0fd594f82c4459.zip
Rollup merge of #127287 - aDotInTheVoid:jsondocck-index, r=GuillaumeGomez
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 57c85bd97da3 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`
-rw-r--r--src/tools/jsondocck/src/main.rs4
1 files changed, 3 insertions, 1 deletions
diff --git a/src/tools/jsondocck/src/main.rs b/src/tools/jsondocck/src/main.rs
index 688b403bf0e..429c6151796 100644
--- a/src/tools/jsondocck/src/main.rs
+++ b/src/tools/jsondocck/src/main.rs
@@ -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,
@@ -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;