Skip to content

Commit 787d704

Browse files
committed
feat: add stricter lints
1 parent 11e06d0 commit 787d704

File tree

3 files changed

+83
-36
lines changed

3 files changed

+83
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
**Features**
44

55
- You can now pass `--use-gh-cli` flag and it will use the [`gh`](https://github.com/cli/cli) CLI. This lets you avoid "Rate limit" errors if you authenticate.
6+
- Environment variable `PATCHY_ROOT` can be set to override where patchy's directory is (by default, uses the nearest ancestor git directory)
67

78
**Breaking**
89

Cargo.toml

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,26 @@ lto = "fat"
7070
# ============================
7171
#
7272
# Lints
73+
#
74+
# - strict set of lints for a
75+
# more consistent codebase
76+
#
77+
# - delegate as much as possible
78+
# to automated tooling
7379
#
7480
# ============================
7581

7682
[lints.rust]
83+
# do not import if its already in scope
84+
# e.g. `use std::option::Option::None`
7785
redundant_imports = "warn"
7886
# Documentation for all public items
7987
missing_docs = "warn"
8088
# `foo::bar::baz` => `bar::baz` if `bar` is in scope
8189
unused_qualifications = "warn"
8290
# detects rules of macros that weren't used
8391
unused_macro_rules = "warn"
84-
# lints against e.g. undefinedd meta variables
92+
# lints against e.g. undefined meta variables
8593
meta_variable_misuse = "warn"
8694
# all types must `#[derive(Copy)]`
8795
missing_copy_implementations = "warn"
@@ -91,20 +99,6 @@ missing_debug_implementations = "warn"
9199
[lints.clippy]
92100
pedantic = { priority = -1, level = "warn" }
93101

94-
# --- allowed lints
95-
#
96-
# `$a * $b + $c` is slower and less precise than `$a.mul_add($b, $c)`
97-
# but it is more readable, the gain in speed / precision
98-
# will be negligible in most situations
99-
suboptimal_flops = "allow"
100-
# arbitrary limit imposes unnecessary
101-
# restriction and can make code harder to follow
102-
too_many_lines = "allow"
103-
# if we need it const, make it const.
104-
# no need to make everything that can be const, const
105-
missing_const_for_fn = "allow"
106-
# ---
107-
108102
# --- more consistent ways of writing code
109103
#
110104
# `if $a { Some($b) } else { None }` => `$a.then(|| $b)`
@@ -118,22 +112,55 @@ redundant_test_prefix = "warn"
118112
# `123832i64` => `123832_i64`
119113
unseparated_literal_suffix = "warn"
120114
# `Foo { a: _, b: 0, .. }` => `Foo { b: 0, .. }`
115+
# do not bind unused by `_` when pattern matching, bind by `..` instead
121116
unneeded_field_pattern = "warn"
117+
# `Err(x)?` => `return Err(x)`
118+
try_err = "warn"
119+
# `#[test] fn` must be in `#[cfg(test)]`
120+
tests_outside_test_module = "warn"
121+
# functions ending in `.and_then` could be better expressed as `?`
122+
return_and_then = "warn"
123+
# `match (A { a }) { A { a, .. } => () }` => `match (A { a: 5 }) { A { a } => () }`
124+
rest_pat_in_fully_bound_structs = "warn"
125+
# do not use differing names from the trait itself when implementing its method
126+
renamed_function_params = "warn"
127+
# `0x2345 & 0xF000 >> 12` => `0x2345 & (0xF000 >> 12)`
128+
precedence_bits = "warn"
129+
# omitting type annotations make code easier to modify
130+
redundant_type_annotations = "warn"
131+
# `assert!(r.is_ok())` => `r.unwrap()`
132+
assertions_on_result_states = "warn"
133+
# `fs::read_to_string` requires much less steps than `File::read_to_string`
134+
verbose_file_reads = "warn"
135+
# `use std::io::{self}` => `use std::io`
136+
unnecessary_self_imports = "warn"
137+
# do not lose type information about NonZero numbers
138+
non_zero_suggestions = "warn"
139+
# exit obscures flow of the program
140+
exit = "warn"
141+
# no need for a `SAFETY:` comment on safe code
142+
unnecessary_safety_comment = "warn"
143+
# each `unsafe` block must contain only 1 unsafe operation
144+
multiple_unsafe_ops_per_block = "warn"
122145
# ---
123146

124147
# --- explain more things
125148
#
126149
# `#[allow]` => `#[allow, reason = "why"]`
127150
allow_attributes_without_reason = "warn"
151+
# `unsafe` blocks need a `SAFETY:` comment
152+
undocumented_unsafe_blocks = "warn"
128153
# `.unwrap()` => `.expect("why")`
129154
unwrap_used = "warn"
155+
# `arr[4]` => `arr.get(4).expect("why")`
156+
indexing_slicing = "warn"
130157
# `assert!(...)` => `assert!(..., "why")`
131158
missing_assert_message = "warn"
132159
# documentation for everything
133160
missing_docs_in_private_items = "warn"
134161
# `path_buf.push("foo")` => `... = PathBuf::new().join("foo")`
135162
pathbuf_init_then_push = "warn"
136-
# mark return type as `!` for infinite loop fns
163+
# explicitly mark return type as `!` for infinite loop fns
137164
infinite_loop = "warn"
138165
# ---
139166

@@ -143,22 +170,34 @@ dbg_macro = "warn"
143170
todo = "warn"
144171
use_debug = "warn"
145172
unimplemented = "warn"
146-
print_stdout = "warn" # > explicitly `#[allow]` functions to print
147-
print_stderr = "warn" # >
173+
# explicitly `#[allow]` functions to print to stdout
174+
print_stdout = "warn"
175+
# explicitly `#[allow]` functions to print to stderr
176+
print_stderr = "warn"
148177
# ---
149178

179+
# --- prevent bugs
180+
# new variants added by libraries become errors
181+
# instead of being silently ignored
182+
wildcard_enum_match_arm = "warn"
183+
# if function and trait provide method of same name, it is confusing
184+
same_name_method = "warn"
150185
# `create_dir(...)` => `create_dir_all(...)`
151186
# usually, failing when dir already exists is
152187
# not what we want
153188
create_dir = "warn"
154-
# `fs::read_to_string` requires much less steps than `File::read_to_string`
155-
verbose_file_reads = "warn"
156-
# new variants added by libraries become errors
157-
# instead of being silently ignored
158-
wildcard_enum_match_arm = "warn"
159-
# `use std::io::{self}` => `use std::io`
160-
unnecessary_self_imports = "warn"
161-
# do not lose type information about NonZero numbers
162-
non_zero_suggestions = "warn"
163-
# exit should only happen from `main`
164-
exit = "warn"
189+
# ---
190+
191+
# --- allowed lints
192+
#
193+
# `$a * $b + $c` is slower and less precise than `$a.mul_add($b, $c)`
194+
# but it is more readable, the gain in speed / precision
195+
# will be negligible in most situations
196+
suboptimal_flops = "allow"
197+
# arbitrary limit imposes unnecessary
198+
# restriction and can make code harder to follow
199+
too_many_lines = "allow"
200+
# if we need it const, make it const.
201+
# no need to make everything that can be const, const
202+
missing_const_for_fn = "allow"
203+
# ---

src/config.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,26 @@ impl FromStr for Ref {
184184
let len = parts.len();
185185

186186
if len == 1 {
187-
// The string does not contain the <syntax>, so the user chose to use the latest
187+
// The string does not contain the ` @ `, so the user chose to use the latest
188188
// commit rather than a specific one
189189
Self {
190190
item: s.into(),
191191
commit: None,
192192
}
193193
} else {
194194
// They want to use a specific commit
195-
let head: String = parts[0..len - 1].iter().map(|s| String::from(*s)).collect();
196-
let commit = (parts[len - 1].to_owned()).parse::<CommitId>().ok();
195+
let head: String = parts
196+
.get(0..len - 1)
197+
.expect("`0..$.len() - 1` is all but the last elemenmt")
198+
.iter()
199+
.map(|s| String::from(*s))
200+
.collect();
201+
let commit = (parts
202+
.last()
203+
.expect("`parts` is always non-empty, even if the split pattern does not match")
204+
.to_owned())
205+
.parse::<CommitId>()
206+
.ok();
197207
Self { item: head, commit }
198208
}
199209
.pipe(Ok)
@@ -330,10 +340,7 @@ pub mod backup {
330340

331341
/// Restore the backed up files
332342
pub fn restore(files: &[FileBackup]) -> Result<()> {
333-
for FileBackup {
334-
filename, contents, ..
335-
} in files
336-
{
343+
for FileBackup { filename, contents } in files {
337344
let path = git::ROOT.join(PathBuf::from(super::ROOT.as_str()).join(filename));
338345
let mut file =
339346
File::create(&path).map_err(|err| anyhow!("failed to restore backup: {err}"))?;

0 commit comments

Comments
 (0)