Skip to content

Commit e8e0126

Browse files
authored
Add comment annotations (#14414)
Fix missing comment annotations in the clippy book changelog: none
2 parents cdf2e7c + 5d7e55c commit e8e0126

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

book/src/development/adding_lints.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,22 @@ struct A;
9999
impl A {
100100
pub fn fo(&self) {}
101101
pub fn foo(&self) {}
102+
//~^ foo_functions
102103
pub fn food(&self) {}
103104
}
104105

105106
// Default trait methods
106107
trait B {
107108
fn fo(&self) {}
108109
fn foo(&self) {}
110+
//~^ foo_functions
109111
fn food(&self) {}
110112
}
111113

112114
// Plain functions
113115
fn fo() {}
114116
fn foo() {}
117+
//~^ foo_functions
115118
fn food() {}
116119

117120
fn main() {
@@ -122,17 +125,20 @@ fn main() {
122125
}
123126
```
124127

125-
Now we can run the test with `TESTNAME=foo_functions cargo uibless`, currently
126-
this test is meaningless though.
128+
Note that we are adding comment annotations with the name of our lint to mark
129+
lines where we expect an error. Once we have implemented our lint we can run
130+
`TESTNAME=foo_functions cargo uibless` to generate the `.stderr` file. If our
131+
lint makes use of structured suggestions then this command will also generate
132+
the corresponding `.fixed` file.
127133

128134
While we are working on implementing our lint, we can keep running the UI test.
129135
That allows us to check if the output is turning into what we want by checking the
130136
`.stderr` file that gets updated on every test run.
131137

132-
Running `TESTNAME=foo_functions cargo uitest` should pass on its own. When we
133-
commit our lint, we need to commit the generated `.stderr` files, too. In
134-
general, you should only commit files changed by `cargo bless` for the
135-
specific lint you are creating/editing.
138+
Once we have implemented our lint running `TESTNAME=foo_functions cargo uitest`
139+
should pass on its own. When we commit our lint, we need to commit the generated
140+
`.stderr` and if applicable `.fixed` files, too. In general, you should only
141+
commit files changed by `cargo bless` for the specific lint you are creating/editing.
136142

137143
> _Note:_ you can run multiple test files by specifying a comma separated list:
138144
> `TESTNAME=foo_functions,test2,test3`.

book/src/development/writing_tests.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,23 @@ Update the file with some examples to get started:
4141
struct A;
4242
impl A {
4343
pub fn fo(&self) {}
44-
pub fn foo(&self) {} //~ ERROR: function called "foo"
44+
pub fn foo(&self) {}
45+
//~^ foo_functions
4546
pub fn food(&self) {}
4647
}
4748

4849
// Default trait methods
4950
trait B {
5051
fn fo(&self) {}
51-
fn foo(&self) {} //~ ERROR: function called "foo"
52+
fn foo(&self) {}
53+
//~^ foo_functions
5254
fn food(&self) {}
5355
}
5456

5557
// Plain functions
5658
fn fo() {}
57-
fn foo() {} //~ ERROR: function called "foo"
59+
fn foo() {}
60+
//~^ foo_functions
5861
fn food() {}
5962

6063
fn main() {
@@ -66,19 +69,38 @@ fn main() {
6669
```
6770

6871
Without actual lint logic to emit the lint when we see a `foo` function name,
69-
this test will just pass, because no lint will be emitted. However, we can now
70-
run the test with the following command:
72+
this test will fail, because we expect errors at lines marked with
73+
`//~^ foo_functions`. However, we can now run the test with the following command:
7174

7275
```sh
7376
$ TESTNAME=foo_functions cargo uitest
7477
```
7578

76-
Clippy will compile and it will conclude with an `ok` for the tests:
79+
Clippy will compile and it will fail complaining it didn't receive any errors:
7780

7881
```
7982
...Clippy warnings and test outputs...
80-
test compile_test ... ok
81-
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.48s
83+
error: diagnostic code `clippy::foo_functions` not found on line 8
84+
--> tests/ui/foo_functions.rs:9:10
85+
|
86+
9 | //~^ foo_functions
87+
| ^^^^^^^^^^^^^ expected because of this pattern
88+
|
89+
90+
error: diagnostic code `clippy::foo_functions` not found on line 16
91+
--> tests/ui/foo_functions.rs:17:10
92+
|
93+
17 | //~^ foo_functions
94+
| ^^^^^^^^^^^^^ expected because of this pattern
95+
|
96+
97+
error: diagnostic code `clippy::foo_functions` not found on line 23
98+
--> tests/ui/foo_functions.rs:24:6
99+
|
100+
24 | //~^ foo_functions
101+
| ^^^^^^^^^^^^^ expected because of this pattern
102+
|
103+
82104
```
83105

84106
This is normal. After all, we wrote a bunch of Rust code but we haven't really

0 commit comments

Comments
 (0)