Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 68a167f

Browse files
committedOct 21, 2023
Hide config implementation details from public docs
1 parent 5728678 commit 68a167f

File tree

13 files changed

+168
-159
lines changed

13 files changed

+168
-159
lines changed
 

‎.github/workflows/clippy_bors.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ jobs:
123123
run: cargo test --features deny-warnings,internal
124124
working-directory: clippy_utils
125125

126+
- name: Test clippy_config
127+
run: cargo test --features deny-warnings
128+
working-directory: clippy_config
129+
126130
- name: Test rustc_tools_util
127131
run: cargo test --features deny-warnings
128132
working-directory: rustc_tools_util

‎Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ rustc_tools_util = "0.3.0"
5555
[features]
5656
deny-warnings = ["clippy_lints/deny-warnings"]
5757
integration = ["tempfile"]
58-
internal = ["clippy_config/internal", "clippy_lints/internal", "tempfile"]
58+
internal = ["clippy_lints/internal", "tempfile"]
5959

6060
[package.metadata.rust-analyzer]
6161
# This package uses #[feature(rustc_private)]

‎book/src/lint_configuration.md

Lines changed: 67 additions & 72 deletions
Large diffs are not rendered by default.

‎clippy_config/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ rustc-semver = "1.1"
1010
serde = { version = "1.0", features = ["derive"] }
1111
toml = "0.7.3"
1212

13+
[dev-dependencies]
14+
walkdir = "2.3"
15+
1316
[features]
1417
deny-warnings = []
15-
internal = []
1618

1719
[package.metadata.rust-analyzer]
1820
# This crate uses #[feature(rustc_private)]

‎clippy_config/src/conf.rs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::ClippyConfiguration;
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_session::Session;
66
use rustc_span::{BytePos, Pos, SourceFile, Span, SyntaxContext};
7-
use serde::de::{Deserializer, IgnoredAny, IntoDeserializer, MapAccess, Visitor};
8-
use serde::Deserialize;
7+
use serde::de::{IgnoredAny, IntoDeserializer, MapAccess, Visitor};
8+
use serde::{Deserialize, Deserializer, Serialize};
99
use std::fmt::{Debug, Display, Formatter};
1010
use std::ops::Range;
1111
use std::path::PathBuf;
@@ -40,10 +40,10 @@ const DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS: &[&str] = &["i", "j", "x", "y", "z
4040

4141
/// Conf with parse errors
4242
#[derive(Default)]
43-
pub struct TryConf {
44-
pub conf: Conf,
45-
pub errors: Vec<ConfError>,
46-
pub warnings: Vec<ConfError>,
43+
struct TryConf {
44+
conf: Conf,
45+
errors: Vec<ConfError>,
46+
warnings: Vec<ConfError>,
4747
}
4848

4949
impl TryConf {
@@ -57,9 +57,9 @@ impl TryConf {
5757
}
5858

5959
#[derive(Debug)]
60-
pub struct ConfError {
61-
pub message: String,
62-
pub span: Span,
60+
struct ConfError {
61+
message: String,
62+
span: Span,
6363
}
6464

6565
impl ConfError {
@@ -81,10 +81,31 @@ impl ConfError {
8181
}
8282
}
8383

84+
macro_rules! wrap_option {
85+
() => {
86+
None
87+
};
88+
($x:literal) => {
89+
Some($x)
90+
};
91+
}
92+
93+
macro_rules! default_text {
94+
($value:expr) => {{
95+
let mut text = String::new();
96+
$value.serialize(toml::ser::ValueSerializer::new(&mut text)).unwrap();
97+
text
98+
}};
99+
($value:expr, $override:expr) => {
100+
$override.to_string()
101+
};
102+
}
103+
84104
macro_rules! define_Conf {
85105
($(
86106
$(#[doc = $doc:literal])+
87107
$(#[conf_deprecated($dep:literal, $new_conf:ident)])?
108+
$(#[default_text = $default_text:expr])?
88109
($name:ident: $ty:ty = $default:expr),
89110
)*) => {
90111
/// Clippy lint configuration
@@ -160,11 +181,6 @@ macro_rules! define_Conf {
160181
}
161182
}
162183

163-
macro_rules! wrap_option {
164-
() => (None);
165-
($x:literal) => (Some($x));
166-
}
167-
168184
pub fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
169185
vec![
170186
$(
@@ -173,8 +189,7 @@ macro_rules! define_Conf {
173189

174190
ClippyConfiguration::new(
175191
stringify!($name),
176-
stringify!($ty),
177-
format!("{:?}", defaults::$name()),
192+
default_text!(defaults::$name() $(, $default_text)?),
178193
concat!($($doc, '\n',)*),
179194
deprecation_reason,
180195
)
@@ -236,7 +251,8 @@ define_Conf! {
236251
(avoid_breaking_exported_api: bool = true),
237252
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, OPTION_MAP_UNWRAP_OR, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT, DEPRECATED_CFG_ATTR, INDEX_REFUTABLE_SLICE, MAP_CLONE, BORROW_AS_PTR, MANUAL_BITS, ERR_EXPECT, CAST_ABS_TO_UNSIGNED, UNINLINED_FORMAT_ARGS, MANUAL_CLAMP, MANUAL_LET_ELSE, UNCHECKED_DURATION_SUBTRACTION, COLLAPSIBLE_STR_REPLACE, SEEK_FROM_CURRENT, SEEK_REWIND, UNNECESSARY_LAZY_EVALUATIONS, TRANSMUTE_PTR_TO_REF, ALMOST_COMPLETE_RANGE, NEEDLESS_BORROW, DERIVABLE_IMPLS, MANUAL_IS_ASCII_CHECK, MANUAL_REM_EUCLID, MANUAL_RETAIN, TYPE_REPETITION_IN_BOUNDS, TUPLE_ARRAY_CONVERSIONS, MANUAL_TRY_FOLD, MANUAL_HASH_ONE.
238253
///
239-
/// The minimum rust version that the project supports
254+
/// The minimum rust version that the project supports. Defaults to the `rust-version` field in `Cargo.toml`
255+
#[default_text = ""]
240256
(msrv: Msrv = Msrv::empty()),
241257
/// DEPRECATED LINT: BLACKLISTED_NAME.
242258
///
@@ -277,8 +293,6 @@ define_Conf! {
277293
/// default configuration of Clippy. By default, any configuration will replace the default value. For example:
278294
/// * `doc-valid-idents = ["ClipPy"]` would replace the default list with `["ClipPy"]`.
279295
/// * `doc-valid-idents = ["ClipPy", ".."]` would append `ClipPy` to the default list.
280-
///
281-
/// Default list:
282296
(doc_valid_idents: Vec<String> = DEFAULT_DOC_VALID_IDENTS.iter().map(ToString::to_string).collect()),
283297
/// Lint: TOO_MANY_ARGUMENTS.
284298
///
@@ -318,7 +332,9 @@ define_Conf! {
318332
(literal_representation_threshold: u64 = 16384),
319333
/// Lint: TRIVIALLY_COPY_PASS_BY_REF.
320334
///
321-
/// The maximum size (in bytes) to consider a `Copy` type for passing by value instead of by reference.
335+
/// The maximum size (in bytes) to consider a `Copy` type for passing by value instead of by
336+
/// reference. By default there is no limit
337+
#[default_text = ""]
322338
(trivial_copy_size_limit: Option<u64> = None),
323339
/// Lint: LARGE_TYPES_PASSED_BY_VALUE.
324340
///
@@ -381,7 +397,7 @@ define_Conf! {
381397
/// Whether the matches should be considered by the lint, and whether there should
382398
/// be filtering for common types.
383399
(matches_for_let_else: MatchLintBehaviour = MatchLintBehaviour::WellKnownTypes),
384-
/// Lint: _CARGO_COMMON_METADATA.
400+
/// Lint: CARGO_COMMON_METADATA.
385401
///
386402
/// For internal testing only, ignores the current `publish` settings in the Cargo manifest.
387403
(cargo_ignore_publish: bool = false),
@@ -742,7 +758,7 @@ mod tests {
742758

743759
#[test]
744760
fn configs_are_tested() {
745-
let mut names: FxHashSet<String> = super::metadata::get_configuration_metadata()
761+
let mut names: FxHashSet<String> = crate::get_configuration_metadata()
746762
.into_iter()
747763
.map(|meta| meta.name.replace('_', "-"))
748764
.collect();

‎clippy_config/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(rustc_private, let_chains)]
22
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
33
#![warn(rust_2018_idioms, unused_lifetimes)]
4+
#![allow(clippy::must_use_candidate, clippy::missing_panics_doc)]
45

56
extern crate rustc_ast;
67
extern crate rustc_data_structures;

‎clippy_config/src/metadata.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
use std::fmt;
1+
use std::fmt::{self, Write};
22

33
#[derive(Debug, Clone, Default)]
44
pub struct ClippyConfiguration {
55
pub name: String,
6-
config_type: &'static str,
76
pub default: String,
87
pub lints: Vec<String>,
98
pub doc: String,
10-
#[allow(dead_code)]
119
pub deprecation_reason: Option<&'static str>,
1210
}
1311

1412
impl fmt::Display for ClippyConfiguration {
1513
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16-
writeln!(
17-
f,
18-
"* `{}`: `{}`(defaults to `{}`): {}",
19-
self.name, self.config_type, self.default, self.doc
20-
)
14+
write!(f, "- `{}`: {}", self.name, self.doc)?;
15+
if !self.default.is_empty() {
16+
write!(f, " (default: `{}`)", self.default)?;
17+
}
18+
Ok(())
2119
}
2220
}
2321

2422
impl ClippyConfiguration {
2523
pub fn new(
2624
name: &'static str,
27-
config_type: &'static str,
2825
default: String,
2926
doc_comment: &'static str,
3027
deprecation_reason: Option<&'static str>,
@@ -36,34 +33,41 @@ impl ClippyConfiguration {
3633
name: to_kebab(name),
3734
lints,
3835
doc,
39-
config_type,
4036
default,
4137
deprecation_reason,
4238
}
4339
}
4440

45-
#[cfg(feature = "internal")]
4641
pub fn to_markdown_paragraph(&self) -> String {
47-
format!(
48-
"## `{}`\n{}\n\n**Default Value:** `{}` (`{}`)\n\n---\n**Affected lints:**\n{}\n\n",
42+
let mut out = format!(
43+
"## `{}`\n{}\n\n",
4944
self.name,
5045
self.doc
5146
.lines()
5247
.map(|line| line.strip_prefix(" ").unwrap_or(line))
5348
.collect::<Vec<_>>()
5449
.join("\n"),
55-
self.default,
56-
self.config_type,
50+
);
51+
52+
if !self.default.is_empty() {
53+
write!(out, "**Default Value:** `{}`\n\n", self.default).unwrap();
54+
}
55+
56+
write!(
57+
out,
58+
"---\n**Affected lints:**\n{}\n\n",
5759
self.lints
5860
.iter()
5961
.map(|name| name.to_string().split_whitespace().next().unwrap().to_string())
6062
.map(|name| format!("* [`{name}`](https://rust-lang.github.io/rust-clippy/master/index.html#{name})"))
6163
.collect::<Vec<_>>()
6264
.join("\n"),
6365
)
66+
.unwrap();
67+
68+
out
6469
}
6570

66-
#[cfg(feature = "internal")]
6771
pub fn to_markdown_link(&self) -> String {
6872
const BOOK_CONFIGS_PATH: &str = "https://doc.rust-lang.org/clippy/lint_configuration.html";
6973
format!("[`{}`]: {BOOK_CONFIGS_PATH}#{}", self.name, self.name)

‎clippy_config/src/msrvs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl Msrv {
105105

106106
if let Some(msrv_attr) = msrv_attrs.next() {
107107
if let Some(duplicate) = msrv_attrs.last() {
108-
sess.struct_span_err(duplicate.span, format!("`clippy::msrv` is defined multiple times"))
108+
sess.struct_span_err(duplicate.span, "`clippy::msrv` is defined multiple times")
109109
.span_note(msrv_attr.span, "first definition found here")
110110
.emit();
111111
}

‎clippy_config/src/types.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::de::{self, Deserializer, Visitor};
2-
use serde::Deserialize;
2+
use serde::{ser, Deserialize, Serialize};
33
use std::fmt;
44
use std::hash::{Hash, Hasher};
55

@@ -33,7 +33,7 @@ impl DisallowedPath {
3333
}
3434
}
3535

36-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize)]
36+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
3737
pub enum MatchLintBehaviour {
3838
AllTypes,
3939
WellKnownTypes,
@@ -117,3 +117,26 @@ impl<'de> Deserialize<'de> for MacroMatcher {
117117
deser.deserialize_struct("MacroMatcher", FIELDS, MacVisitor)
118118
}
119119
}
120+
121+
// these impls are never actually called but are used by the various config options that default to
122+
// empty lists
123+
macro_rules! unimplemented_serialize {
124+
($($t:ty,)*) => {
125+
$(
126+
impl Serialize for $t {
127+
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
128+
where
129+
S: ser::Serializer,
130+
{
131+
Err(ser::Error::custom("unimplemented"))
132+
}
133+
}
134+
)*
135+
}
136+
}
137+
138+
unimplemented_serialize! {
139+
DisallowedPath,
140+
Rename,
141+
MacroMatcher,
142+
}

‎clippy_lints/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ walkdir = "2.3"
3535
[features]
3636
deny-warnings = ["clippy_utils/deny-warnings"]
3737
# build clippy with internal lints enabled, off by default
38-
internal = ["clippy_config/internal", "clippy_utils/internal", "serde_json", "tempfile", "regex"]
38+
internal = ["clippy_utils/internal", "serde_json", "tempfile", "regex"]
3939

4040
[package.metadata.rust-analyzer]
4141
# This crate uses #[feature(rustc_private)]

‎clippy_lints/src/lib.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ mod zero_div_zero;
357357
mod zero_sized_map_values;
358358
// end lints modules, do not remove this comment, it’s used in `update_lints`
359359

360-
use crate::utils::FindAll;
361360
use clippy_config::{get_configuration_metadata, Conf};
362361

363362
/// Register all pre expansion lints
@@ -460,16 +459,13 @@ pub fn explain(name: &str) -> i32 {
460459
if let Some(info) = declared_lints::LINTS.iter().find(|info| info.lint.name == target) {
461460
println!("{}", info.explanation);
462461
// Check if the lint has configuration
463-
let mdconf = get_configuration_metadata();
464-
if let Some(config_vec_positions) = mdconf
465-
.iter()
466-
.find_all(|cconf| cconf.lints.contains(&info.lint.name_lower()[8..].to_owned()))
467-
{
468-
// If it has, print it
462+
let mut mdconf = get_configuration_metadata();
463+
let name = name.to_ascii_lowercase();
464+
mdconf.retain(|cconf| cconf.lints.contains(&name));
465+
if !mdconf.is_empty() {
469466
println!("### Configuration for {}:\n", info.lint.name_lower());
470-
for position in config_vec_positions {
471-
let conf = &mdconf[position];
472-
println!(" - {}: {} (default: {})", conf.name, conf.doc, conf.default);
467+
for conf in mdconf {
468+
println!("{conf}");
473469
}
474470
}
475471
0

‎clippy_lints/src/utils/mod.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,3 @@ pub mod dump_hir;
33
pub mod format_args_collector;
44
#[cfg(feature = "internal")]
55
pub mod internal_lints;
6-
7-
// ==================================================================
8-
// Configuration
9-
// ==================================================================
10-
11-
// Shamelessly stolen from find_all (https://github.com/nectariner/find_all)
12-
pub trait FindAll: Iterator + Sized {
13-
fn find_all<P>(&mut self, predicate: P) -> Option<Vec<usize>>
14-
where
15-
P: FnMut(&Self::Item) -> bool;
16-
}
17-
18-
impl<I> FindAll for I
19-
where
20-
I: Iterator,
21-
{
22-
fn find_all<P>(&mut self, mut predicate: P) -> Option<Vec<usize>>
23-
where
24-
P: FnMut(&Self::Item) -> bool,
25-
{
26-
let mut occurences = Vec::<usize>::default();
27-
for (index, element) in self.enumerate() {
28-
if predicate(&element) {
29-
occurences.push(index);
30-
}
31-
}
32-
33-
match occurences.len() {
34-
0 => None,
35-
_ => Some(occurences),
36-
}
37-
}
38-
}

‎tests/dogfood.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fn dogfood_clippy() {
2828
"clippy_dev",
2929
"clippy_lints",
3030
"clippy_utils",
31+
"clippy_config",
3132
"lintcheck",
3233
"rustc_tools_util",
3334
] {

0 commit comments

Comments
 (0)
Please sign in to comment.