Skip to content

Commit c3db082

Browse files
authored
Rollup merge of #6308 - matthiaskrgr:internal_lints, r=flip1995
add `internal-lints` feature to enable clippys internal lints (off by default) This PR moves the internal lint tests into a new subdirectory (I couldn't find a different way to compile-time-conditionally exclude them from compiletest) and only builds and tests internal lints if the `internal-lints` feature is enabled. Fixes #6306 changelog: put internal lints behind a feature ("internal-lints")
2 parents 4785da6 + 252083f commit c3db082

27 files changed

+102
-46
lines changed

.github/workflows/clippy_bors.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ jobs:
128128
SYSROOT=$(rustc --print sysroot)
129129
echo "$SYSROOT/bin" >> $GITHUB_PATH
130130
131-
- name: Build
132-
run: cargo build --features deny-warnings
131+
- name: Build with internal lints
132+
run: cargo build --features deny-warnings,internal-lints
133133

134-
- name: Test
135-
run: cargo test --features deny-warnings
134+
- name: Test with internal lints
135+
run: cargo test --features deny-warnings,internal-lints
136136

137-
- name: Test clippy_lints
138-
run: cargo test --features deny-warnings
137+
- name: Test clippy_lints with internal lints
138+
run: cargo test --features deny-warnings,internal-lints
139139
working-directory: clippy_lints
140140

141141
- 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-9
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,30 @@ 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+
let other_lints = usable_lints
165+
.sorted_by_key(|l| format!(" &{}::{},", l.module, l.name.to_uppercase()))
153166
.map(|l| format!(" &{}::{},", l.module, l.name.to_uppercase()))
154-
.sorted()
155-
.collect::<Vec<String>>();
156-
inner.insert(0, pre);
157-
inner.push(post);
158-
inner
167+
.sorted();
168+
let mut lint_list = vec![header];
169+
lint_list.extend(internal_lints);
170+
lint_list.extend(other_lints);
171+
lint_list.push(footer);
172+
lint_list
159173
}
160174

161175
/// 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
@@ -68,7 +68,7 @@ pub fn run(update_mode: UpdateMode) {
6868
"end register lints",
6969
false,
7070
update_mode == UpdateMode::Change,
71-
|| gen_register_lint_list(usable_lints.iter().chain(internal_lints.iter())),
71+
|| gen_register_lint_list(internal_lints.iter(), usable_lints.iter()),
7272
)
7373
.changed;
7474

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

+34-21
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,24 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
498498

499499
// begin register lints, do not remove this comment, it’s used in `update_lints`
500500
store.register_lints(&[
501+
#[cfg(feature = "internal-lints")]
502+
&utils::internal_lints::CLIPPY_LINTS_INTERNAL,
503+
#[cfg(feature = "internal-lints")]
504+
&utils::internal_lints::COLLAPSIBLE_SPAN_LINT_CALLS,
505+
#[cfg(feature = "internal-lints")]
506+
&utils::internal_lints::COMPILER_LINT_FUNCTIONS,
507+
#[cfg(feature = "internal-lints")]
508+
&utils::internal_lints::DEFAULT_LINT,
509+
#[cfg(feature = "internal-lints")]
510+
&utils::internal_lints::INVALID_PATHS,
511+
#[cfg(feature = "internal-lints")]
512+
&utils::internal_lints::LINT_WITHOUT_LINT_PASS,
513+
#[cfg(feature = "internal-lints")]
514+
&utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM,
515+
#[cfg(feature = "internal-lints")]
516+
&utils::internal_lints::OUTER_EXPN_EXPN_DATA,
517+
#[cfg(feature = "internal-lints")]
518+
&utils::internal_lints::PRODUCE_ICE,
501519
&approx_const::APPROX_CONSTANT,
502520
&arithmetic::FLOAT_ARITHMETIC,
503521
&arithmetic::INTEGER_ARITHMETIC,
@@ -904,15 +922,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
904922
&unwrap_in_result::UNWRAP_IN_RESULT,
905923
&use_self::USE_SELF,
906924
&useless_conversion::USELESS_CONVERSION,
907-
&utils::internal_lints::CLIPPY_LINTS_INTERNAL,
908-
&utils::internal_lints::COLLAPSIBLE_SPAN_LINT_CALLS,
909-
&utils::internal_lints::COMPILER_LINT_FUNCTIONS,
910-
&utils::internal_lints::DEFAULT_LINT,
911-
&utils::internal_lints::INVALID_PATHS,
912-
&utils::internal_lints::LINT_WITHOUT_LINT_PASS,
913-
&utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM,
914-
&utils::internal_lints::OUTER_EXPN_EXPN_DATA,
915-
&utils::internal_lints::PRODUCE_ICE,
916925
&vec::USELESS_VEC,
917926
&vec_resize_to_zero::VEC_RESIZE_TO_ZERO,
918927
&verbose_file_reads::VERBOSE_FILE_READS,
@@ -930,14 +939,23 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
930939
&zero_div_zero::ZERO_DIVIDED_BY_ZERO,
931940
]);
932941
// end register lints, do not remove this comment, it’s used in `update_lints`
942+
943+
// all the internal lints
944+
#[cfg(feature = "internal-lints")]
945+
{
946+
store.register_early_pass(|| box utils::internal_lints::ClippyLintsInternal);
947+
store.register_early_pass(|| box utils::internal_lints::ProduceIce);
948+
store.register_late_pass(|| box utils::inspector::DeepCodeInspector);
949+
store.register_late_pass(|| box utils::internal_lints::CollapsibleCalls);
950+
store.register_late_pass(|| box utils::internal_lints::CompilerLintFunctions::new());
951+
store.register_late_pass(|| box utils::internal_lints::InvalidPaths);
952+
store.register_late_pass(|| box utils::internal_lints::LintWithoutLintPass::default());
953+
store.register_late_pass(|| box utils::internal_lints::MatchTypeOnDiagItem);
954+
store.register_late_pass(|| box utils::internal_lints::OuterExpnDataPass);
955+
}
956+
store.register_late_pass(|| box utils::author::Author);
933957
store.register_late_pass(|| box await_holding_invalid::AwaitHolding);
934958
store.register_late_pass(|| box serde_api::SerdeAPI);
935-
store.register_late_pass(|| box utils::internal_lints::CompilerLintFunctions::new());
936-
store.register_late_pass(|| box utils::internal_lints::LintWithoutLintPass::default());
937-
store.register_late_pass(|| box utils::internal_lints::OuterExpnDataPass);
938-
store.register_late_pass(|| box utils::internal_lints::InvalidPaths);
939-
store.register_late_pass(|| box utils::inspector::DeepCodeInspector);
940-
store.register_late_pass(|| box utils::author::Author);
941959
let vec_box_size_threshold = conf.vec_box_size_threshold;
942960
store.register_late_pass(move || box types::Types::new(vec_box_size_threshold));
943961
store.register_late_pass(|| box booleans::NonminimalBool);
@@ -1122,7 +1140,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11221140
store.register_early_pass(|| box literal_representation::LiteralDigitGrouping);
11231141
let literal_representation_threshold = conf.literal_representation_threshold;
11241142
store.register_early_pass(move || box literal_representation::DecimalLiteralRepresentation::new(literal_representation_threshold));
1125-
store.register_early_pass(|| box utils::internal_lints::ClippyLintsInternal);
11261143
let enum_variant_name_threshold = conf.enum_variant_name_threshold;
11271144
store.register_early_pass(move || box enum_variants::EnumVariantNames::new(enum_variant_name_threshold));
11281145
store.register_early_pass(|| box tabs_in_doc_comments::TabsInDocComments);
@@ -1136,7 +1153,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11361153
store.register_late_pass(move || box large_const_arrays::LargeConstArrays::new(array_size_threshold));
11371154
store.register_late_pass(|| box floating_point_arithmetic::FloatingPointArithmetic);
11381155
store.register_early_pass(|| box as_conversions::AsConversions);
1139-
store.register_early_pass(|| box utils::internal_lints::ProduceIce);
11401156
store.register_late_pass(|| box let_underscore::LetUnderscore);
11411157
store.register_late_pass(|| box atomic_ordering::AtomicOrdering);
11421158
store.register_early_pass(|| box single_component_path_imports::SingleComponentPathImports);
@@ -1152,15 +1168,13 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11521168
store.register_late_pass(|| box dereference::Dereferencing);
11531169
store.register_late_pass(|| box option_if_let_else::OptionIfLetElse);
11541170
store.register_late_pass(|| box future_not_send::FutureNotSend);
1155-
store.register_late_pass(|| box utils::internal_lints::CollapsibleCalls);
11561171
store.register_late_pass(|| box if_let_mutex::IfLetMutex);
11571172
store.register_late_pass(|| box mut_mutex_lock::MutMutexLock);
11581173
store.register_late_pass(|| box match_on_vec_items::MatchOnVecItems);
11591174
store.register_late_pass(|| box manual_async_fn::ManualAsyncFn);
11601175
store.register_early_pass(|| box redundant_field_names::RedundantFieldNames);
11611176
store.register_late_pass(|| box vec_resize_to_zero::VecResizeToZero);
11621177
store.register_late_pass(|| box panic_in_result_fn::PanicInResultFn);
1163-
11641178
let single_char_binding_names_threshold = conf.single_char_binding_names_threshold;
11651179
store.register_early_pass(move || box non_expressive_names::NonExpressiveNames {
11661180
single_char_binding_names_threshold,
@@ -1177,7 +1191,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11771191
store.register_late_pass(|| box manual_ok_or::ManualOkOr);
11781192
store.register_late_pass(|| box float_equality_without_abs::FloatEqualityWithoutAbs);
11791193
store.register_late_pass(|| box async_yields_async::AsyncYieldsAsync);
1180-
store.register_late_pass(|| box utils::internal_lints::MatchTypeOnDiagItem);
11811194
let disallowed_methods = conf.disallowed_methods.iter().cloned().collect::<FxHashSet<_>>();
11821195
store.register_late_pass(move || box disallowed_method::DisallowedMethod::new(&disallowed_methods));
11831196
store.register_early_pass(|| box asm_syntax::InlineAsmX86AttSyntax);
@@ -1186,7 +1199,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11861199
store.register_late_pass(|| box strings::StrToString);
11871200
store.register_late_pass(|| box strings::StringToString);
11881201

1189-
11901202
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
11911203
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
11921204
LintId::of(&arithmetic::INTEGER_ARITHMETIC),
@@ -1318,6 +1330,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13181330
LintId::of(&wildcard_imports::WILDCARD_IMPORTS),
13191331
]);
13201332

1333+
#[cfg(feature = "internal-lints")]
13211334
store.register_group(true, "clippy::internal", Some("clippy_internal"), vec![
13221335
LintId::of(&utils::internal_lints::CLIPPY_LINTS_INTERNAL),
13231336
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"];
@@ -61,8 +62,10 @@ pub const IO_WRITE: [&str; 3] = ["std", "io", "Write"];
6162
pub const IPADDR_V4: [&str; 4] = ["std", "net", "IpAddr", "V4"];
6263
pub const IPADDR_V6: [&str; 4] = ["std", "net", "IpAddr", "V6"];
6364
pub const ITERATOR: [&str; 5] = ["core", "iter", "traits", "iterator", "Iterator"];
65+
#[cfg(feature = "internal-lints")]
6466
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
6567
pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"];
68+
#[cfg(feature = "internal-lints")]
6669
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];
6770
pub const MEM_DISCRIMINANT: [&str; 3] = ["core", "mem", "discriminant"];
6871
pub const MEM_FORGET: [&str; 3] = ["core", "mem", "forget"];
@@ -133,6 +136,7 @@ pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "<impl str>", "ends_with"];
133136
pub const STR_FROM_UTF8: [&str; 4] = ["core", "str", "converts", "from_utf8"];
134137
pub const STR_LEN: [&str; 4] = ["core", "str", "<impl str>", "len"];
135138
pub const STR_STARTS_WITH: [&str; 4] = ["core", "str", "<impl str>", "starts_with"];
139+
#[cfg(feature = "internal-lints")]
136140
pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"];
137141
pub const TO_OWNED: [&str; 3] = ["alloc", "borrow", "ToOwned"];
138142
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

+11-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ fn dogfood_clippy() {
1818
}
1919
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
2020

21-
let output = Command::new(&*CLIPPY_PATH)
21+
let mut command = Command::new(&*CLIPPY_PATH);
22+
command
2223
.current_dir(root_dir)
2324
.env("CLIPPY_DOGFOOD", "1")
2425
.env("CARGO_INCREMENTAL", "0")
@@ -27,11 +28,16 @@ fn dogfood_clippy() {
2728
.arg("--all-features")
2829
.arg("--")
2930
.args(&["-D", "clippy::all"])
30-
.args(&["-D", "clippy::internal"])
3131
.args(&["-D", "clippy::pedantic"])
32-
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
33-
.output()
34-
.unwrap();
32+
.arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
33+
34+
// internal lints only exist if we build with the internal-lints feature
35+
if cfg!(feature = "internal-lints") {
36+
command.args(&["-D", "clippy::internal"]);
37+
}
38+
39+
let output = command.output().unwrap();
40+
3541
println!("status: {}", output.status);
3642
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
3743
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)