Skip to content

Commit 933a0a9

Browse files
committed
Auto merge of rust-lang#5156 - flip1995:dev_add_internal, r=phansch
Let update_lints also generate the internal lints r? @phansch changelog: none
2 parents bdd4046 + 50a2f97 commit 933a0a9

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

clippy_dev/src/lib.rs

+29-21
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ impl Lint {
6161
lints.filter(|l| l.deprecation.is_none() && !l.is_internal())
6262
}
6363

64+
/// Returns all internal lints (not `internal_warn` lints)
65+
pub fn internal_lints(lints: impl Iterator<Item = Self>) -> impl Iterator<Item = Self> {
66+
lints.filter(|l| l.group == "internal")
67+
}
68+
6469
/// Returns the lints in a `HashMap`, grouped by the different lint groups
6570
#[must_use]
66-
pub fn by_lint_group(lints: &[Self]) -> HashMap<String, Vec<Self>> {
67-
lints
68-
.iter()
69-
.map(|lint| (lint.group.to_string(), lint.clone()))
70-
.into_group_map()
71+
pub fn by_lint_group(lints: impl Iterator<Item = Self>) -> HashMap<String, Vec<Self>> {
72+
lints.map(|lint| (lint.group.to_string(), lint)).into_group_map()
7173
}
7274

7375
#[must_use]
@@ -82,7 +84,7 @@ pub fn gen_lint_group_list(lints: Vec<Lint>) -> Vec<String> {
8284
lints
8385
.into_iter()
8486
.filter_map(|l| {
85-
if l.is_internal() || l.deprecation.is_some() {
87+
if l.deprecation.is_some() {
8688
None
8789
} else {
8890
Some(format!(" LintId::of(&{}::{}),", l.module, l.name.to_uppercase()))
@@ -173,29 +175,34 @@ pub fn gather_all() -> impl Iterator<Item = Lint> {
173175

174176
fn gather_from_file(dir_entry: &walkdir::DirEntry) -> impl Iterator<Item = Lint> {
175177
let content = fs::read_to_string(dir_entry.path()).unwrap();
176-
let mut filename = dir_entry.path().file_stem().unwrap().to_str().unwrap();
178+
let path = dir_entry.path();
179+
let filename = path.file_stem().unwrap();
180+
let path_buf = path.with_file_name(filename);
181+
let mut rel_path = path_buf
182+
.strip_prefix(clippy_project_root().join("clippy_lints/src"))
183+
.expect("only files in `clippy_lints/src` should be looked at");
177184
// If the lints are stored in mod.rs, we get the module name from
178185
// the containing directory:
179186
if filename == "mod" {
180-
filename = dir_entry
181-
.path()
182-
.parent()
183-
.unwrap()
184-
.file_stem()
185-
.unwrap()
186-
.to_str()
187-
.unwrap()
187+
rel_path = rel_path.parent().unwrap();
188188
}
189-
parse_contents(&content, filename)
189+
190+
let module = rel_path
191+
.components()
192+
.map(|c| c.as_os_str().to_str().unwrap())
193+
.collect::<Vec<_>>()
194+
.join("::");
195+
196+
parse_contents(&content, &module)
190197
}
191198

192-
fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item = Lint> {
199+
fn parse_contents(content: &str, module: &str) -> impl Iterator<Item = Lint> {
193200
let lints = DEC_CLIPPY_LINT_RE
194201
.captures_iter(content)
195-
.map(|m| Lint::new(&m["name"], &m["cat"], &m["desc"], None, filename));
202+
.map(|m| Lint::new(&m["name"], &m["cat"], &m["desc"], None, module));
196203
let deprecated = DEC_DEPRECATED_LINT_RE
197204
.captures_iter(content)
198-
.map(|m| Lint::new(&m["name"], "Deprecated", &m["desc"], Some(&m["desc"]), filename));
205+
.map(|m| Lint::new(&m["name"], "Deprecated", &m["desc"], Some(&m["desc"]), module));
199206
// Removing the `.collect::<Vec<Lint>>().into_iter()` causes some lifetime issues due to the map
200207
lints.chain(deprecated).collect::<Vec<Lint>>().into_iter()
201208
}
@@ -449,7 +456,7 @@ fn test_by_lint_group() {
449456
"group2".to_string(),
450457
vec![Lint::new("should_assert_eq2", "group2", "abc", None, "module_name")],
451458
);
452-
assert_eq!(expected, Lint::by_lint_group(&lints));
459+
assert_eq!(expected, Lint::by_lint_group(lints.into_iter()));
453460
}
454461

455462
#[test]
@@ -522,10 +529,11 @@ fn test_gen_lint_group_list() {
522529
Lint::new("abc", "group1", "abc", None, "module_name"),
523530
Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
524531
Lint::new("should_assert_eq2", "group2", "abc", Some("abc"), "deprecated"),
525-
Lint::new("incorrect_internal", "internal_style", "abc", None, "module_name"),
532+
Lint::new("internal", "internal_style", "abc", None, "module_name"),
526533
];
527534
let expected = vec![
528535
" LintId::of(&module_name::ABC),".to_string(),
536+
" LintId::of(&module_name::INTERNAL),".to_string(),
529537
" LintId::of(&module_name::SHOULD_ASSERT_EQ),".to_string(),
530538
];
531539
assert_eq!(expected, gen_lint_group_list(lints));

clippy_dev/src/main.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ fn main() {
135135
fn print_lints() {
136136
let lint_list = gather_all();
137137
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list).collect();
138-
let lint_count = usable_lints.len();
139-
let grouped_by_lint_group = Lint::by_lint_group(&usable_lints);
138+
let usable_lint_count = usable_lints.len();
139+
let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
140140

141141
for (lint_group, mut lints) in grouped_by_lint_group {
142142
if lint_group == "Deprecated" {
@@ -157,15 +157,17 @@ fn print_lints() {
157157
}
158158
}
159159

160-
println!("there are {} lints", lint_count);
160+
println!("there are {} lints", usable_lint_count);
161161
}
162162

163163
#[allow(clippy::too_many_lines)]
164164
fn update_lints(update_mode: UpdateMode) {
165165
let lint_list: Vec<Lint> = gather_all().collect();
166166

167+
let internal_lints = Lint::internal_lints(lint_list.clone().into_iter());
168+
167169
let usable_lints: Vec<Lint> = Lint::usable_lints(lint_list.clone().into_iter()).collect();
168-
let lint_count = usable_lints.len();
170+
let usable_lint_count = usable_lints.len();
169171

170172
let mut sorted_usable_lints = usable_lints.clone();
171173
sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
@@ -198,7 +200,7 @@ fn update_lints(update_mode: UpdateMode) {
198200
|| {
199201
vec![format!(
200202
"[There are {} lints included in this crate!]({})",
201-
lint_count, DOCS_LINK
203+
usable_lint_count, DOCS_LINK
202204
)]
203205
},
204206
)
@@ -267,7 +269,7 @@ fn update_lints(update_mode: UpdateMode) {
267269
.changed;
268270

269271
// Generate the list of lints for all other lint groups
270-
for (lint_group, lints) in Lint::by_lint_group(&usable_lints) {
272+
for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
271273
file_change |= replace_region_in_file(
272274
Path::new("clippy_lints/src/lib.rs"),
273275
&format!("store.register_group\\(true, \"clippy::{}\"", lint_group),

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1105,10 +1105,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11051105
store.register_group(true, "clippy::internal", Some("clippy_internal"), vec![
11061106
LintId::of(&utils::internal_lints::CLIPPY_LINTS_INTERNAL),
11071107
LintId::of(&utils::internal_lints::COMPILER_LINT_FUNCTIONS),
1108+
LintId::of(&utils::internal_lints::DEFAULT_LINT),
11081109
LintId::of(&utils::internal_lints::LINT_WITHOUT_LINT_PASS),
11091110
LintId::of(&utils::internal_lints::OUTER_EXPN_EXPN_DATA),
11101111
LintId::of(&utils::internal_lints::PRODUCE_ICE),
1111-
LintId::of(&utils::internal_lints::DEFAULT_LINT),
11121112
]);
11131113

11141114
store.register_group(true, "clippy::all", Some("clippy"), vec![

doc/adding_lints.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,15 @@ declare_lint_pass!(FooFunctions => [FOO_FUNCTIONS]);
191191
impl EarlyLintPass for FooFunctions {}
192192
```
193193

194-
Don't worry about the `name` method here. As long as it includes the name of the
195-
lint pass it should be fine.
196-
197-
The new lint automation runs `update_lints`, which automates some things, but it
198-
doesn't automate everything. We will have to register our lint pass manually in
199-
the `register_plugins` function in `clippy_lints/src/lib.rs`:
194+
Normally after declaring the lint, we have to run `cargo dev update_lints`,
195+
which updates some files, so Clippy knows about the new lint. Since we used
196+
`cargo dev new_lint ...` to generate the lint declaration, this was done
197+
automatically. While `update_lints` automates most of the things, it doesn't
198+
automate everything. We will have to register our lint pass manually in the
199+
`register_plugins` function in `clippy_lints/src/lib.rs`:
200200

201201
```rust
202-
reg.register_early_lint_pass(box foo_functions::FooFunctions);
202+
store.register_early_pass(box foo_functions::FooFunctions);
203203
```
204204

205205
This should fix the `unknown clippy lint: clippy::foo_functions` error that we

0 commit comments

Comments
 (0)