Commit Graph

45 Commits

Author SHA1 Message Date
Martin Fischer 6649225167 rule 8/8: Automatically rewrite RuleCode to Rule
# This commit was automatically generated by running the following
# script (followed by `cargo +nightly fmt`):

import glob
import re
from typing import NamedTuple

class Rule(NamedTuple):
    code: str
    name: str
    path: str

def rules() -> list[Rule]:
    """Returns all the rules defined in `src/registry.rs`."""
    file = open('src/registry.rs')

    rules = []

    while next(file) != 'ruff_macros::define_rule_mapping!(\n':
        continue

    while (line := next(file)) != ');\n':
        line = line.strip().rstrip(',')
        if line.startswith('//'):
            continue
        code, path = line.split(' => ')
        name = path.rsplit('::')[-1]
        rules.append(Rule(code, name, path))

    return rules

code2name = {r.code: r.name for r in rules()}

for pattern in ('src/**/*.rs', 'ruff_cli/**/*.rs', 'ruff_dev/**/*.rs', 'scripts/add_*.py'):
    for name in glob.glob(pattern, recursive=True):
        with open(name) as f:
            text = f.read()

        text = re.sub('Rule(?:Code)?::([A-Z]\w+)', lambda m: 'Rule::' + code2name[m.group(1)], text)
        text = re.sub(r'(?<!"<FilePattern>:<)RuleCode\b', 'Rule', text)
        text = re.sub('(use crate::registry::{.*, Rule), Rule(.*)', r'\1\2', text) # fix duplicate import

        with open(name, 'w') as f:
            f.write(text)
2023-01-18 23:51:48 -05:00
Martin Fischer 8c18b28bc4 Derive Hash instead of implementing it by hand
The caching mechanism of the CLI (ruff_cli::cache) relies on
ruff::settings::Settings implementing the Hash trait.

The ruff::settings::Settings struct previously couldn't automatically
derive the Hash implementation via the #[derive(Hash)] macro attribute
since some of its field types intentionally[1][2] don't implement Hash
(namely regex::Regex, globset::GlobMatcher and globset::GlobSet and
HashMap and HashSet from the standard library).

The code therefore previously implemented the Hash trait by hand for the
whole struct. Implementing Hash by hand for structs that are subject to
change is a bad idea since it's very easy to forget to update the Hash
implementation when adding a new field to the struct. And the Hash
implementation indeed was already incorrect by omitting several fields
from the hash.

This commit introduces wrapper types for Regex, GlobMatcher, GlobSet,
HashSet & HashMap that implement Hash so that we can still add
#[derive(Hash)] to the Settings struct, guaranteeing a correct hash
implementation.

[1]: https://github.com/rust-lang/regex/issues/364#issuecomment-301082076
[2]: The standard library doesn't impl<T: Hash + Ord> Hash for HashSet<T>
     presumably since sorted() requires an allocation and Hash
     implementations are generally expected to work without allocations.
2023-01-16 01:42:55 -05:00
Martin Fischer 82aff5f9ec Split off ruff_cli crate from ruff library
This lets you test the ruff linters or use the ruff library
without having to compile the ~100 additional dependencies
that are needed by the CLI.

Because we set the following in the [workspace] section of Cargo.toml:

   default-members = [".", "ruff_cli"]

`cargo run` still runs the CLI and `cargo test` still tests
the code in src/ as well as the code in the new ruff_cli crate.
(But you can now also run `cargo test -p ruff` to only test the linters.)
2023-01-13 21:37:54 -05:00
Charlie Marsh 09dc3c7225
Rename `Check` to `Diagnostic` (#1725)
Along with:

- `CheckKind` -> `DiagnosticKind`
- `CheckCode` -> `RuleCode`
- `CheckCodePrefix` -> `RuleCodePrefix`
2023-01-08 17:46:20 -05:00
Martin Fischer 6a723b50c7 structs 8/9: Run cargo fix and cargo fmt 2023-01-07 15:14:58 -05:00
Martin Fischer 43db446dfa structs 6/9: Automatically change CheckKind::* to violations::*
The changes in this commit were generated by running:

for f in $(find src -name '*.rs'); do sed -Ei 's/use crate::registry::.*;/\0use crate::violations;/g' $f; done
for f in $(find src -name '*.rs'); do sed -Ei 's/CheckKind::([A-Z])/violations::\1/g' $f; done
git checkout src/registry.rs src/lib.rs src/lib_wasm.rs src/violations.rs
cargo +nightly fmt
2023-01-07 15:14:58 -05:00
Charlie Marsh 8647bec3cb
Rename checks.rs to registry.rs (#1566) 2023-01-02 17:26:51 -05:00
Charlie Marsh 2393e270ed Change a few more methods to take AsRef<Path> 2022-12-16 21:38:52 -05:00
Charlie Marsh f36e6035c8 Change a few methods to take AsRef<Path> 2022-12-16 21:28:19 -05:00
Charlie Marsh 0157fedab5
Move Python file resolution into resolver.rs (#1211) 2022-12-12 10:43:50 -05:00
Charlie Marsh cd69610741
Use `--config` everywhere if provided (#1210) 2022-12-12 10:28:00 -05:00
Charlie Marsh 73794fc299
Resolve hierarchical settings and Python files in a single filesystem pass (#1205) 2022-12-12 10:13:52 -05:00
Charlie Marsh 0adc9ed259
Support hierarchical settings for nested directories (#1190) 2022-12-12 10:12:23 -05:00
Charlie Marsh 35fa2a3c32
Convert more BTree usages to Fx (#1112) 2022-12-07 12:21:12 -05:00
Charlie Marsh 0acc47386a
Use pyproject.toml parent as project root when explicitly provided (#1101) 2022-12-05 23:00:59 -05:00
CelebrateVC a3af6c1ea5
Implement GlobSet optimization for file path exclusions (#883) 2022-11-24 22:31:55 -05:00
Anders Kaseorg 0bb8b14ae1 Fix clippy::single_match_else (pedantic)
https://rust-lang.github.io/rust-clippy/master/index.html#single_match_else

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-11-21 23:22:28 -05:00
Anders Kaseorg 6b935121a7 Fix clippy::explicit_deref_methods (pedantic)
https://rust-lang.github.io/rust-clippy/master/index.html#explicit_deref_methods

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-11-21 23:22:28 -05:00
Anders Kaseorg b657d912d9
Propagate errors from glob::Pattern::new (#858) 2022-11-21 13:39:19 -05:00
Charlie Marsh 965918744b
Replace FNV with rustc-hash (#837) 2022-11-20 15:38:31 -05:00
Charlie Marsh 7d8360a1de
Change all &Option<> to Option<&> (#768) 2022-11-16 09:40:01 -05:00
Charlie Marsh 71f727c380
Use FNV hasher in more places (#732) 2022-11-13 23:44:16 -05:00
Charlie Marsh 2727e5f3b7
Remove some usages of Ruff internals in ruff_dev (#610) 2022-11-05 16:07:21 -04:00
Charlie Marsh 79ca66ace5
Use nightly rustfmt with rustfmt.toml (#536) 2022-11-01 20:34:38 -04:00
Charlie Marsh 7df903dc4d
Move around and rename some of the Settings structs (#496) 2022-10-28 18:46:54 -04:00
Charlie Marsh 8fc5e91ec7
Enable prefix-based check code selection (#493) 2022-10-28 18:19:57 -04:00
Anders Kaseorg 91666fcaf6
Don’t follow directory symlinks found while walking (#280) 2022-09-29 15:10:25 -04:00
Seamooo 02ae494a0e
Enable per-file ignores (#261) 2022-09-24 13:02:34 -04:00
Anders Kaseorg 401b53cc45
Handle filesystem errors more consistently (#240) 2022-09-20 23:22:01 -04:00
Anders Kaseorg f7fc702b2c
Include specified files, even if they lack a .py[i] extension (#238) 2022-09-20 20:53:52 -04:00
Charlie Marsh 4ed88dd245
Follow-up fixes to path absolution (#235) 2022-09-20 12:26:32 -04:00
Charlie Marsh fa0954fe47
Treat relative excludes as relative to project root (#228) 2022-09-19 20:45:02 -06:00
Charlie Marsh a0b50d7ebc
Use absolute paths for exclusion matching (#213) 2022-09-19 20:32:31 -06:00
Charlie Marsh 9d4a4478f7
Improve exclusion syntax to match exact files (#209) 2022-09-15 21:40:49 -04:00
Charlie Marsh d008a181ec
Improve default exclusions and support extend-exclude (#188) 2022-09-14 22:21:17 -04:00
Charlie Marsh 59f009b52d
Enable globs in excludes list (#64) 2022-08-31 18:53:13 -04:00
Charlie Marsh 7ed5b3d3a2
Avoid re-reading + iterating over lines for ignores (#48) 2022-08-30 13:19:22 -04:00
Charlie Marsh b11a7eefa3
Enable excludes (#18) 2022-08-20 13:00:58 -04:00
Charlie Marsh 7359e862c1
Add pyproject.toml support (#17) 2022-08-20 13:00:34 -04:00
Charlie Marsh b7d7c50b2b
Add benchmarking (#16) 2022-08-19 15:38:43 -04:00
Charlie Marsh 3b1b53dacf
Add support for noqa pragmas (#11) 2022-08-15 22:00:50 -04:00
Charlie Marsh 611037bbd4
Add a check for long lines (#10) 2022-08-13 18:02:30 -04:00
Charles Marsh 8b2a5f94ae Rename to iter_python_files 2022-08-13 13:53:31 -04:00
Charlie Marsh 8091beca89
Implement visitor pattern (#4) 2022-08-12 17:53:31 -04:00
Charles Marsh 6257dd00c7 Support multiple files 2022-08-09 17:03:16 -04:00