Skip to content

Commit ab8552a

Browse files
committed
Auto merge of #6308 - matthiaskrgr:internal_lints, r=<try>
WIP: add internal-lints feature to enable clippys internal lints (off by default) I'm getting liker errors (but not compiler warnings) when building locally, checking if this affects CI as well... :/ changelog: put internal lints behind a feature
2 parents bd13a35 + ba3f1ae commit ab8552a

27 files changed

+126
-45
lines changed

.github/workflows/clippy_bors.yml

+11-4
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,18 @@ jobs:
131131
- name: Build
132132
run: cargo build --features deny-warnings
133133

134-
- name: Test
135-
run: cargo test --features deny-warnings
134+
# compiletest would panic due to "Found multiple rlibs for crate `clippy_lints`"
135+
- name: clean rlibs
136+
run: rm -f ./target/debug/deps/libclippy_lints*
136137

137-
- name: Test clippy_lints
138-
run: cargo test --features deny-warnings
138+
- name: Build with internal lints
139+
run: cargo build --features deny-warnings internal-lints
140+
141+
- name: Test with internal lints
142+
run: cargo test --features deny-warnings --features internal-lints
143+
144+
- name: Test clippy_lints with internal lints
145+
run: cargo test --features deny-warnings --features internal-lints
139146
working-directory: clippy_lints
140147

141148
- name: Test rustc_tools_util

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ path = "src/driver.rs"
3232
clippy_lints = { version = "0.0.212", path = "clippy_lints" }
3333
# end automatic update
3434
semver = "0.11"
35-
rustc_tools_util = { version = "0.2.0", path = "rustc_tools_util"}
35+
rustc_tools_util = { version = "0.2.0", path = "rustc_tools_util" }
3636
tempfile = { version = "3.1.0", optional = true }
3737

3838
[dev-dependencies]
@@ -49,8 +49,9 @@ derive-new = "0.5"
4949
rustc-workspace-hack = "1.0.0"
5050

5151
[build-dependencies]
52-
rustc_tools_util = { version = "0.2.0", path = "rustc_tools_util"}
52+
rustc_tools_util = { version = "0.2.0", path = "rustc_tools_util" }
5353

5454
[features]
5555
deny-warnings = []
5656
integration = ["tempfile"]
57+
internal-lints = ["clippy_lints/internal-lints"]

clippy_dev/src/lib.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,32 @@ pub fn gen_deprecated<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String>
146146
}
147147

148148
#[must_use]
149-
pub fn gen_register_lint_list<'a>(lints: impl Iterator<Item = &'a Lint>) -> Vec<String> {
150-
let pre = " store.register_lints(&[".to_string();
151-
let post = " ]);".to_string();
152-
let mut inner = lints
149+
pub fn gen_register_lint_list<'a>(
150+
internal_lints: impl Iterator<Item = &'a Lint>,
151+
usable_lints: impl Iterator<Item = &'a Lint>,
152+
) -> Vec<String> {
153+
let header = " store.register_lints(&[".to_string();
154+
let footer = " ]);".to_string();
155+
let internal_lints = internal_lints
156+
.sorted_by_key(|l| format!(" &{}::{},", l.module, l.name.to_uppercase()))
157+
.map(|l| {
158+
format!(
159+
" #[cfg(feature = \"internal-lints\")]\n &{}::{},",
160+
l.module,
161+
l.name.to_uppercase()
162+
)
163+
})
164+
.collect::<Vec<String>>();
165+
let other_lints = usable_lints
166+
.sorted_by_key(|l| format!(" &{}::{},", l.module, l.name.to_uppercase()))
153167
.map(|l| format!(" &{}::{},", l.module, l.name.to_uppercase()))
154168
.sorted()
155169
.collect::<Vec<String>>();
156-
inner.insert(0, pre);
157-
inner.push(post);
158-
inner
170+
let mut lint_list = vec![header];
171+
lint_list.extend(internal_lints);
172+
lint_list.extend(other_lints);
173+
lint_list.push(footer);
174+
lint_list
159175
}
160176

161177
/// Gathers all files in `src/clippy_lints` and gathers all lints inside

clippy_dev/src/update_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn run(update_mode: UpdateMode) {
8181
"end register lints",
8282
false,
8383
update_mode == UpdateMode::Change,
84-
|| gen_register_lint_list(usable_lints.iter().chain(internal_lints.iter())),
84+
|| gen_register_lint_list(internal_lints.iter(), usable_lints.iter()),
8585
)
8686
.changed;
8787

clippy_lints/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ syn = { version = "1", features = ["full"] }
3636

3737
[features]
3838
deny-warnings = []
39+
# build clippy with internal lints enabled, off by default
40+
internal-lints = []

clippy_lints/src/lib.rs

+31-15
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,24 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
499499

500500
// begin register lints, do not remove this comment, it’s used in `update_lints`
501501
store.register_lints(&[
502+
#[cfg(feature = "internal-lints")]
503+
&utils::internal_lints::CLIPPY_LINTS_INTERNAL,
504+
#[cfg(feature = "internal-lints")]
505+
&utils::internal_lints::COLLAPSIBLE_SPAN_LINT_CALLS,
506+
#[cfg(feature = "internal-lints")]
507+
&utils::internal_lints::COMPILER_LINT_FUNCTIONS,
508+
#[cfg(feature = "internal-lints")]
509+
&utils::internal_lints::DEFAULT_LINT,
510+
#[cfg(feature = "internal-lints")]
511+
&utils::internal_lints::INVALID_PATHS,
512+
#[cfg(feature = "internal-lints")]
513+
&utils::internal_lints::LINT_WITHOUT_LINT_PASS,
514+
#[cfg(feature = "internal-lints")]
515+
&utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM,
516+
#[cfg(feature = "internal-lints")]
517+
&utils::internal_lints::OUTER_EXPN_EXPN_DATA,
518+
#[cfg(feature = "internal-lints")]
519+
&utils::internal_lints::PRODUCE_ICE,
502520
&approx_const::APPROX_CONSTANT,
503521
&arithmetic::FLOAT_ARITHMETIC,
504522
&arithmetic::INTEGER_ARITHMETIC,
@@ -902,15 +920,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
902920
&unwrap_in_result::UNWRAP_IN_RESULT,
903921
&use_self::USE_SELF,
904922
&useless_conversion::USELESS_CONVERSION,
905-
&utils::internal_lints::CLIPPY_LINTS_INTERNAL,
906-
&utils::internal_lints::COLLAPSIBLE_SPAN_LINT_CALLS,
907-
&utils::internal_lints::COMPILER_LINT_FUNCTIONS,
908-
&utils::internal_lints::DEFAULT_LINT,
909-
&utils::internal_lints::INVALID_PATHS,
910-
&utils::internal_lints::LINT_WITHOUT_LINT_PASS,
911-
&utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM,
912-
&utils::internal_lints::OUTER_EXPN_EXPN_DATA,
913-
&utils::internal_lints::PRODUCE_ICE,
914923
&vec::USELESS_VEC,
915924
&vec_resize_to_zero::VEC_RESIZE_TO_ZERO,
916925
&verbose_file_reads::VERBOSE_FILE_READS,
@@ -931,11 +940,14 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
931940

932941
store.register_late_pass(|| box await_holding_invalid::AwaitHolding);
933942
store.register_late_pass(|| box serde_api::SerdeAPI);
934-
store.register_late_pass(|| box utils::internal_lints::CompilerLintFunctions::new());
935-
store.register_late_pass(|| box utils::internal_lints::LintWithoutLintPass::default());
936-
store.register_late_pass(|| box utils::internal_lints::OuterExpnDataPass);
937-
store.register_late_pass(|| box utils::internal_lints::InvalidPaths);
938-
store.register_late_pass(|| box utils::inspector::DeepCodeInspector);
943+
#[cfg(feature = "internal-lints")]
944+
{
945+
store.register_late_pass(|| box utils::internal_lints::CompilerLintFunctions::new());
946+
store.register_late_pass(|| box utils::internal_lints::LintWithoutLintPass::default());
947+
store.register_late_pass(|| box utils::internal_lints::OuterExpnDataPass);
948+
store.register_late_pass(|| box utils::internal_lints::InvalidPaths);
949+
store.register_late_pass(|| box utils::inspector::DeepCodeInspector);
950+
}
939951
store.register_late_pass(|| box utils::author::Author);
940952
let vec_box_size_threshold = conf.vec_box_size_threshold;
941953
store.register_late_pass(move || box types::Types::new(vec_box_size_threshold));
@@ -1104,6 +1116,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11041116
store.register_early_pass(|| box literal_representation::LiteralDigitGrouping);
11051117
let literal_representation_threshold = conf.literal_representation_threshold;
11061118
store.register_early_pass(move || box literal_representation::DecimalLiteralRepresentation::new(literal_representation_threshold));
1119+
#[cfg(feature = "internal-lints")]
11071120
store.register_early_pass(|| box utils::internal_lints::ClippyLintsInternal);
11081121
let enum_variant_name_threshold = conf.enum_variant_name_threshold;
11091122
store.register_early_pass(move || box enum_variants::EnumVariantNames::new(enum_variant_name_threshold));
@@ -1118,6 +1131,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11181131
store.register_late_pass(move || box large_const_arrays::LargeConstArrays::new(array_size_threshold));
11191132
store.register_late_pass(|| box floating_point_arithmetic::FloatingPointArithmetic);
11201133
store.register_early_pass(|| box as_conversions::AsConversions);
1134+
#[cfg(feature = "internal-lints")]
11211135
store.register_early_pass(|| box utils::internal_lints::ProduceIce);
11221136
store.register_late_pass(|| box let_underscore::LetUnderscore);
11231137
store.register_late_pass(|| box atomic_ordering::AtomicOrdering);
@@ -1134,6 +1148,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11341148
store.register_late_pass(|| box dereference::Dereferencing);
11351149
store.register_late_pass(|| box option_if_let_else::OptionIfLetElse);
11361150
store.register_late_pass(|| box future_not_send::FutureNotSend);
1151+
#[cfg(feature = "internal-lints")]
11371152
store.register_late_pass(|| box utils::internal_lints::CollapsibleCalls);
11381153
store.register_late_pass(|| box if_let_mutex::IfLetMutex);
11391154
store.register_late_pass(|| box mut_mutex_lock::MutMutexLock);
@@ -1161,6 +1176,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11611176
store.register_late_pass(|| box float_equality_without_abs::FloatEqualityWithoutAbs);
11621177
store.register_late_pass(|| box async_yields_async::AsyncYieldsAsync);
11631178
store.register_late_pass(|| box manual_strip::ManualStrip);
1179+
#[cfg(feature = "internal-lints")]
11641180
store.register_late_pass(|| box utils::internal_lints::MatchTypeOnDiagItem);
11651181
let disallowed_methods = conf.disallowed_methods.iter().cloned().collect::<FxHashSet<_>>();
11661182
store.register_late_pass(move || box disallowed_method::DisallowedMethod::new(&disallowed_methods));
@@ -1295,7 +1311,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12951311
LintId::of(&wildcard_imports::ENUM_GLOB_USE),
12961312
LintId::of(&wildcard_imports::WILDCARD_IMPORTS),
12971313
]);
1298-
1314+
#[cfg(feature = "internal-lints")]
12991315
store.register_group(true, "clippy::internal", Some("clippy_internal"), vec![
13001316
LintId::of(&utils::internal_lints::CLIPPY_LINTS_INTERNAL),
13011317
LintId::of(&utils::internal_lints::COLLAPSIBLE_SPAN_LINT_CALLS),

clippy_lints/src/utils/diagnostics.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ pub fn span_lint_hir_and_then(
186186
/// |
187187
/// = note: `-D fold-any` implied by `-D warnings`
188188
/// ```
189-
#[allow(clippy::collapsible_span_lint_calls)]
189+
190+
#[allow(clippy::unknown_clippy_lints)]
191+
#[cfg_attr(feature = "internal-lints", allow(clippy::collapsible_span_lint_calls))]
190192
pub fn span_lint_and_sugg<'a, T: LintContext>(
191193
cx: &'a T,
192194
lint: &'static Lint,

clippy_lints/src/utils/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod eager_or_lazy;
1414
pub mod higher;
1515
mod hir_utils;
1616
pub mod inspector;
17+
#[cfg(feature = "internal-lints")]
1718
pub mod internal_lints;
1819
pub mod numeric_literal;
1920
pub mod paths;

clippy_lints/src/utils/paths.rs

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub const DISPLAY_TRAIT: [&str; 3] = ["core", "fmt", "Display"];
3131
pub const DOUBLE_ENDED_ITERATOR: [&str; 4] = ["core", "iter", "traits", "DoubleEndedIterator"];
3232
pub const DROP: [&str; 3] = ["core", "mem", "drop"];
3333
pub const DURATION: [&str; 3] = ["core", "time", "Duration"];
34+
#[cfg(feature = "internal-lints")]
3435
pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"];
3536
pub const EXIT: [&str; 3] = ["std", "process", "exit"];
3637
pub const F32_EPSILON: [&str; 4] = ["core", "f32", "<impl f32>", "EPSILON"];
@@ -59,8 +60,10 @@ pub const INTO_ITERATOR: [&str; 5] = ["core", "iter", "traits", "collect", "Into
5960
pub const IO_READ: [&str; 3] = ["std", "io", "Read"];
6061
pub const IO_WRITE: [&str; 3] = ["std", "io", "Write"];
6162
pub const ITERATOR: [&str; 5] = ["core", "iter", "traits", "iterator", "Iterator"];
63+
#[cfg(feature = "internal-lints")]
6264
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
6365
pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"];
66+
#[cfg(feature = "internal-lints")]
6467
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];
6568
pub const MEM_DISCRIMINANT: [&str; 3] = ["core", "mem", "discriminant"];
6669
pub const MEM_FORGET: [&str; 3] = ["core", "mem", "forget"];
@@ -125,6 +128,7 @@ pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "<impl str>", "ends_with"];
125128
pub const STR_FROM_UTF8: [&str; 4] = ["core", "str", "converts", "from_utf8"];
126129
pub const STR_LEN: [&str; 4] = ["core", "str", "<impl str>", "len"];
127130
pub const STR_STARTS_WITH: [&str; 4] = ["core", "str", "<impl str>", "starts_with"];
131+
#[cfg(feature = "internal-lints")]
128132
pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"];
129133
pub const TO_OWNED: [&str; 3] = ["alloc", "borrow", "ToOwned"];
130134
pub const TO_OWNED_METHOD: [&str; 4] = ["alloc", "borrow", "ToOwned", "to_owned"];

tests/compile-test.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use std::path::{Path, PathBuf};
1212

1313
mod cargo;
1414

15+
// whether to run internal tests or not
16+
const RUN_INTERNAL_TESTS: bool = cfg!(feature = "internal-lints");
17+
1518
fn host_lib() -> PathBuf {
1619
option_env!("HOST_LIBS").map_or(cargo::CARGO_TARGET_DIR.join(env!("PROFILE")), PathBuf::from)
1720
}
@@ -96,6 +99,16 @@ fn run_mode(cfg: &mut compiletest::Config) {
9699
compiletest::run_tests(&cfg);
97100
}
98101

102+
fn run_internal_tests(cfg: &mut compiletest::Config) {
103+
// only run internal tests with the internal-tests feature
104+
if !RUN_INTERNAL_TESTS {
105+
return;
106+
}
107+
cfg.mode = TestMode::Ui;
108+
cfg.src_base = Path::new("tests").join("ui-internal");
109+
compiletest::run_tests(&cfg);
110+
}
111+
99112
fn run_ui_toml(config: &mut compiletest::Config) {
100113
fn run_tests(config: &compiletest::Config, mut tests: Vec<tester::TestDescAndFn>) -> Result<bool, io::Error> {
101114
let mut result = true;
@@ -199,7 +212,6 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
199212
Some("main.rs") => {},
200213
_ => continue,
201214
}
202-
203215
let paths = compiletest::common::TestPaths {
204216
file: file_path,
205217
base: config.src_base.clone(),
@@ -253,4 +265,5 @@ fn compile_test() {
253265
run_mode(&mut config);
254266
run_ui_toml(&mut config);
255267
run_ui_cargo(&mut config);
268+
run_internal_tests(&mut config);
256269
}

tests/dogfood.rs

+33-14
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,39 @@ fn dogfood_clippy() {
1818
}
1919
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
2020

21-
let output = Command::new(&*CLIPPY_PATH)
22-
.current_dir(root_dir)
23-
.env("CLIPPY_DOGFOOD", "1")
24-
.env("CARGO_INCREMENTAL", "0")
25-
.arg("clippy-preview")
26-
.arg("--all-targets")
27-
.arg("--all-features")
28-
.arg("--")
29-
.args(&["-D", "clippy::all"])
30-
.args(&["-D", "clippy::internal"])
31-
.args(&["-D", "clippy::pedantic"])
32-
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
33-
.output()
34-
.unwrap();
21+
let output = if cfg!(feature = "internal-lints") {
22+
// with internal lints and internal warnings
23+
Command::new(&*CLIPPY_PATH)
24+
.current_dir(root_dir)
25+
.env("CLIPPY_DOGFOOD", "1")
26+
.env("CARGO_INCREMENTAL", "0")
27+
.arg("clippy-preview")
28+
.arg("--all-targets")
29+
.arg("--all-features")
30+
.args(&["--features", "internal-lints"])
31+
.arg("--")
32+
.args(&["-D", "clippy::all"])
33+
.args(&["-D", "clippy::pedantic"])
34+
.args(&["-D", "clippy::internal"])
35+
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
36+
.output()
37+
.unwrap()
38+
} else {
39+
// without internal lints or warnings
40+
Command::new(&*CLIPPY_PATH)
41+
.current_dir(root_dir)
42+
.env("CLIPPY_DOGFOOD", "1")
43+
.env("CARGO_INCREMENTAL", "0")
44+
.arg("clippy-preview")
45+
.arg("--all-targets")
46+
.arg("--all-features")
47+
.arg("--")
48+
.args(&["-D", "clippy::all"])
49+
.args(&["-D", "clippy::pedantic"])
50+
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
51+
.output()
52+
.unwrap()
53+
};
3554
println!("status: {}", output.status);
3655
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
3756
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)