Skip to content

Commit

Permalink
Auto merge of #13173 - Jarcho:conf_order, r=xFrednet
Browse files Browse the repository at this point in the history
Misc changes to `clippy_config`

Contains part of #13084

Changes include:
* Sort config list and each configs lint list.
* Add default text for the two configs that were missing it.
* Switch the lint list in the configs to an attribute.
* Make `dev fmt` sort the config list.

r? `@xFrednet`

changelog: none
  • Loading branch information
bors committed Jul 29, 2024
2 parents f7db895 + 78a750e commit 3b64ca9
Show file tree
Hide file tree
Showing 8 changed files with 671 additions and 561 deletions.
6 changes: 5 additions & 1 deletion book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ Suppress lints whenever the suggested change would cause breakage for other crat


## `await-holding-invalid-types`

The list of types which may not be held across an await point.

**Default Value:** `[]`

Expand Down Expand Up @@ -668,6 +668,8 @@ crate. For example, `pub(crate)` items.
## `msrv`
The minimum rust version that the project supports. Defaults to the `rust-version` field in `Cargo.toml`

**Default Value:** `current version`

---
**Affected lints:**
* [`allow_attributes`](https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes)
Expand Down Expand Up @@ -862,6 +864,8 @@ The maximum number of lines a function or method can have
The maximum size (in bytes) to consider a `Copy` type for passing by value instead of by
reference. By default there is no limit

**Default Value:** `target_pointer_width * 2`

---
**Affected lints:**
* [`trivially_copy_pass_by_ref`](https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref)
Expand Down
1 change: 1 addition & 0 deletions clippy_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
itertools = "0.12"
rustc-semver = "1.1"
serde = { version = "1.0", features = ["derive"] }
toml = "0.7.3"
Expand Down
724 changes: 360 additions & 364 deletions clippy_config/src/conf.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion clippy_config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(rustc_private, let_chains)]
#![feature(rustc_private, array_windows, let_chains)]
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
#![warn(
trivial_casts,
Expand Down
103 changes: 11 additions & 92 deletions clippy_config/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::fmt::{self, Write};
use itertools::Itertools;
use std::fmt;

#[derive(Debug, Clone, Default)]
pub struct ClippyConfiguration {
pub name: String,
pub default: String,
pub lints: Vec<String>,
pub doc: String,
pub lints: &'static [&'static str],
pub doc: &'static str,
pub deprecation_reason: Option<&'static str>,
}

Expand All @@ -20,102 +21,20 @@ impl fmt::Display for ClippyConfiguration {
}

impl ClippyConfiguration {
pub fn new(
name: &'static str,
default: String,
doc_comment: &'static str,
deprecation_reason: Option<&'static str>,
) -> Self {
let (mut lints, doc) = parse_config_field_doc(doc_comment)
.unwrap_or_else(|| (vec![], "[ERROR] MALFORMED DOC COMMENT".to_string()));

lints.sort();

Self {
name: to_kebab(name),
lints,
doc,
default,
deprecation_reason,
}
}

pub fn to_markdown_paragraph(&self) -> String {
let mut out = format!(
"## `{}`\n{}\n\n",
format!(
"## `{}`\n{}\n\n**Default Value:** `{}`\n\n---\n**Affected lints:**\n{}\n\n",
self.name,
self.doc
.lines()
.map(|line| line.strip_prefix(" ").unwrap_or(line))
.collect::<Vec<_>>()
.join("\n"),
);

if !self.default.is_empty() {
write!(out, "**Default Value:** `{}`\n\n", self.default).unwrap();
}

write!(
out,
"---\n**Affected lints:**\n{}\n\n",
self.lints
.iter()
.map(|name| name.to_string().split_whitespace().next().unwrap().to_string())
.map(|name| format!("* [`{name}`](https://rust-lang.github.io/rust-clippy/master/index.html#{name})"))
.collect::<Vec<_>>()
.join("\n"),
self.doc.lines().map(|x| x.strip_prefix(' ').unwrap_or(x)).join("\n"),
self.default,
self.lints.iter().format_with("\n", |name, f| f(&format_args!(
"* [`{name}`](https://rust-lang.github.io/rust-clippy/master/index.html#{name})"
))),
)
.unwrap();

out
}

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

/// This parses the field documentation of the config struct.
///
/// ```rust, ignore
/// parse_config_field_doc(cx, "Lint: LINT_NAME_1, LINT_NAME_2. Papa penguin, papa penguin")
/// ```
///
/// Would yield:
/// ```rust, ignore
/// Some(["lint_name_1", "lint_name_2"], "Papa penguin, papa penguin")
/// ```
fn parse_config_field_doc(doc_comment: &str) -> Option<(Vec<String>, String)> {
const DOC_START: &str = " Lint: ";
if doc_comment.starts_with(DOC_START)
&& let Some(split_pos) = doc_comment.find('.')
{
let mut doc_comment = doc_comment.to_string();
let mut documentation = doc_comment.split_off(split_pos);

// Extract lints
doc_comment.make_ascii_lowercase();
let lints: Vec<String> = doc_comment
.split_off(DOC_START.len())
.lines()
.next()
.unwrap()
.split(", ")
.map(str::to_string)
.collect();

// Format documentation correctly
// split off leading `.` from lint name list and indent for correct formatting
documentation = documentation.trim_start_matches('.').trim().replace("\n ", "\n ");

Some((lints, documentation))
} else {
None
}
}

/// Transforms a given `snake_case_string` to a tasty `kebab-case-string`
fn to_kebab(config_name: &str) -> String {
config_name.replace('_', "-")
}
Loading

0 comments on commit 3b64ca9

Please sign in to comment.