Skip to content

Experiment: Move lint docs into markdown files #6992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions clippy_lints/docs/correctness/APPROX_CONSTANT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
**What it does:** Checks for floating point literals that approximate
constants which are defined in
[`std::f32::consts`](https://doc.rust-lang.org/stable/std/f32/consts/#constants)
or
[`std::f64::consts`](https://doc.rust-lang.org/stable/std/f64/consts/#constants),
respectively, suggesting to use the predefined constant.

**Why is this bad?** Usually, the definition in the standard library is more
precise than what people come up with. If you find that your definition is
actually more precise, please [file a Rust
issue](https://github.com/rust-lang/rust/issues).

**Known problems:** None.

**Example:**
```rust
let x = 3.14;
let y = 1_f64 / x;
```
Use predefined constants instead:
```rust
let x = std::f32::consts::PI;
let y = std::f64::consts::FRAC_1_PI;
```
19 changes: 19 additions & 0 deletions clippy_lints/docs/restriction/INTEGER_ARITHMETIC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
**What it does:** Checks for integer arithmetic operations which could overflow or panic.

Specifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable
of overflowing according to the [Rust
Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),
or which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is
attempted.

**Why is this bad?** Integer overflow will trigger a panic in debug builds or will wrap in
release mode. Division by zero will cause a panic in either mode. In some applications one
wants explicitly checked, wrapping or saturating arithmetic.

**Known problems:** None.

**Example:**
```rust
# let a = 0;
a + 1;
```
26 changes: 1 addition & 25 deletions clippy_lints/src/approx_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol;
use std::f64::consts as f64;

declare_clippy_lint! {
/// **What it does:** Checks for floating point literals that approximate
/// constants which are defined in
/// [`std::f32::consts`](https://doc.rust-lang.org/stable/std/f32/consts/#constants)
/// or
/// [`std::f64::consts`](https://doc.rust-lang.org/stable/std/f64/consts/#constants),
/// respectively, suggesting to use the predefined constant.
///
/// **Why is this bad?** Usually, the definition in the standard library is more
/// precise than what people come up with. If you find that your definition is
/// actually more precise, please [file a Rust
/// issue](https://github.com/rust-lang/rust/issues).
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// let x = 3.14;
/// let y = 1_f64 / x;
/// ```
/// Use predefined constants instead:
/// ```rust
/// let x = std::f32::consts::PI;
/// let y = std::f64::consts::FRAC_1_PI;
/// ```
declare_clippy_lint_new! {
pub APPROX_CONSTANT,
correctness,
"the approximate of a known float constant (in `std::fXX::consts`)"
Expand Down
21 changes: 1 addition & 20 deletions clippy_lints/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,7 @@ use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::Span;

declare_clippy_lint! {
/// **What it does:** Checks for integer arithmetic operations which could overflow or panic.
///
/// Specifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable
/// of overflowing according to the [Rust
/// Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),
/// or which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is
/// attempted.
///
/// **Why is this bad?** Integer overflow will trigger a panic in debug builds or will wrap in
/// release mode. Division by zero will cause a panic in either mode. In some applications one
/// wants explicitly checked, wrapping or saturating arithmetic.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// # let a = 0;
/// a + 1;
/// ```
declare_clippy_lint_new! {
pub INTEGER_ARITHMETIC,
restriction,
"any integer arithmetic expression which could overflow or panic"
Expand Down
11 changes: 11 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(drain_filter)]
#![feature(extended_key_value_attributes)]
#![feature(in_band_lifetimes)]
#![feature(once_cell)]
#![feature(rustc_private)]
Expand Down Expand Up @@ -145,6 +146,16 @@ macro_rules! declare_clippy_lint {
};
}

macro_rules! declare_clippy_lint_new {
{ $(#[$attr:meta])* pub $name:tt, $category:tt $($tail:tt)* } => {
declare_clippy_lint! {
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/docs/",
stringify!($category), "/", stringify!($name), ".md"))]
$(#[$attr])* pub $name, $category $($tail)*
}
};
}

#[macro_export]
macro_rules! sym {
( $($x:tt)* ) => { clippy_utils::sym!($($x)*) }
Expand Down
10 changes: 10 additions & 0 deletions util/lintlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def parse_lints(lints, filepath):
comment = []
clippy = False
deprecated = False
externaldoc = False
name = ""

with open(filepath) as fp:
Expand Down Expand Up @@ -60,11 +61,14 @@ def parse_lints(lints, filepath):

log.info("found %s with level %s in %s",
name, level, filepath)
if externaldoc:
comment = open(f"clippy_lints/docs/{group}/{name}.md").read().splitlines()
lints.append(Lint(name, level, comment, filepath, group))
comment = []

clippy = False
deprecated = False
externaldoc = False
name = ""
else:
m = comment_re.search(line)
Expand All @@ -73,9 +77,15 @@ def parse_lints(lints, filepath):
elif line.startswith("declare_clippy_lint!"):
clippy = True
deprecated = False
externaldoc = False
elif line.startswith("declare_clippy_lint_new!"):
clippy = True
deprecated = False
externaldoc = True
elif line.startswith("declare_deprecated_lint!"):
clippy = False
deprecated = True
externaldoc = False
elif line.startswith("declare_lint!"):
import sys
print(
Expand Down