Skip to content

Commit 594b232

Browse files
authored
Accept commas in default copyright pattern (#9498)
## Summary Adds commas as an accepted separator between copyright years by default, which is actually documented in one spot, but not currently accurate. Fixes #9477.
1 parent a06ffeb commit 594b232

12 files changed

+192
-6
lines changed

crates/ruff/tests/snapshots/show_settings__display_default_settings.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ linter.flake8_bandit.check_typed_exception = false
231231
linter.flake8_bugbear.extend_immutable_calls = []
232232
linter.flake8_builtins.builtins_ignorelist = []
233233
linter.flake8_comprehensions.allow_dict_calls_with_keyword_arguments = false
234-
linter.flake8_copyright.notice_rgx = (?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}(-\d{4})*
234+
linter.flake8_copyright.notice_rgx = (?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}((-|,\s)\d{4})*
235235
linter.flake8_copyright.author = none
236236
linter.flake8_copyright.min_file_size = 0
237237
linter.flake8_errmsg.max_string_length = 0

crates/ruff_linter/src/rules/flake8_copyright/mod.rs

+134
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ import os
7171
r"
7272
# Copyright (C) 2021-2023
7373
74+
import os
75+
"
76+
.trim(),
77+
&settings::LinterSettings::for_rules(vec![Rule::MissingCopyrightNotice]),
78+
);
79+
assert_messages!(diagnostics);
80+
}
81+
82+
#[test]
83+
fn notice_with_comma() {
84+
let diagnostics = test_snippet(
85+
r"
86+
# Copyright (C) 2021, 2022
87+
7488
import os
7589
"
7690
.trim(),
@@ -85,6 +99,126 @@ import os
8599
r"
86100
# Copyright (C) 2023 Ruff
87101
102+
import os
103+
"
104+
.trim(),
105+
&settings::LinterSettings {
106+
flake8_copyright: super::settings::Settings {
107+
author: Some("Ruff".to_string()),
108+
..super::settings::Settings::default()
109+
},
110+
..settings::LinterSettings::for_rules(vec![Rule::MissingCopyrightNotice])
111+
},
112+
);
113+
assert_messages!(diagnostics);
114+
}
115+
116+
#[test]
117+
fn valid_author_with_dash() {
118+
let diagnostics = test_snippet(
119+
r"
120+
# Copyright (C) 2022-2023 Ruff
121+
122+
import os
123+
"
124+
.trim(),
125+
&settings::LinterSettings {
126+
flake8_copyright: super::settings::Settings {
127+
author: Some("Ruff".to_string()),
128+
..super::settings::Settings::default()
129+
},
130+
..settings::LinterSettings::for_rules(vec![Rule::MissingCopyrightNotice])
131+
},
132+
);
133+
assert_messages!(diagnostics);
134+
}
135+
136+
#[test]
137+
fn valid_author_with_dash_invalid_space() {
138+
let diagnostics = test_snippet(
139+
r"
140+
# Copyright (C) 2022- 2023 Ruff
141+
142+
import os
143+
"
144+
.trim(),
145+
&settings::LinterSettings {
146+
flake8_copyright: super::settings::Settings {
147+
author: Some("Ruff".to_string()),
148+
..super::settings::Settings::default()
149+
},
150+
..settings::LinterSettings::for_rules(vec![Rule::MissingCopyrightNotice])
151+
},
152+
);
153+
assert_messages!(diagnostics);
154+
}
155+
156+
#[test]
157+
fn valid_author_with_dash_invalid_spaces() {
158+
let diagnostics = test_snippet(
159+
r"
160+
# Copyright (C) 2022 - 2023 Ruff
161+
162+
import os
163+
"
164+
.trim(),
165+
&settings::LinterSettings {
166+
flake8_copyright: super::settings::Settings {
167+
author: Some("Ruff".to_string()),
168+
..super::settings::Settings::default()
169+
},
170+
..settings::LinterSettings::for_rules(vec![Rule::MissingCopyrightNotice])
171+
},
172+
);
173+
assert_messages!(diagnostics);
174+
}
175+
176+
#[test]
177+
fn valid_author_with_comma_invalid_no_space() {
178+
let diagnostics = test_snippet(
179+
r"
180+
# Copyright (C) 2022,2023 Ruff
181+
182+
import os
183+
"
184+
.trim(),
185+
&settings::LinterSettings {
186+
flake8_copyright: super::settings::Settings {
187+
author: Some("Ruff".to_string()),
188+
..super::settings::Settings::default()
189+
},
190+
..settings::LinterSettings::for_rules(vec![Rule::MissingCopyrightNotice])
191+
},
192+
);
193+
assert_messages!(diagnostics);
194+
}
195+
196+
#[test]
197+
fn valid_author_with_comma_invalid_spaces() {
198+
let diagnostics = test_snippet(
199+
r"
200+
# Copyright (C) 2022 , 2023 Ruff
201+
202+
import os
203+
"
204+
.trim(),
205+
&settings::LinterSettings {
206+
flake8_copyright: super::settings::Settings {
207+
author: Some("Ruff".to_string()),
208+
..super::settings::Settings::default()
209+
},
210+
..settings::LinterSettings::for_rules(vec![Rule::MissingCopyrightNotice])
211+
},
212+
);
213+
assert_messages!(diagnostics);
214+
}
215+
216+
#[test]
217+
fn valid_author_with_comma_valid_space() {
218+
let diagnostics = test_snippet(
219+
r"
220+
# Copyright (C) 2022, 2023 Ruff
221+
88222
import os
89223
"
90224
.trim(),

crates/ruff_linter/src/rules/flake8_copyright/settings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct Settings {
1515
}
1616

1717
pub static COPYRIGHT: Lazy<Regex> =
18-
Lazy::new(|| Regex::new(r"(?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}(-\d{4})*").unwrap());
18+
Lazy::new(|| Regex::new(r"(?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}((-|,\s)\d{4})*").unwrap());
1919

2020
impl Default for Settings {
2121
fn default() -> Self {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_copyright/mod.rs
3+
---
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_copyright/mod.rs
3+
---
4+
<filename>:1:1: CPY001 Missing copyright notice at top of file
5+
|
6+
1 | # Copyright (C) 2022,2023 Ruff
7+
| CPY001
8+
2 |
9+
3 | import os
10+
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_copyright/mod.rs
3+
---
4+
<filename>:1:1: CPY001 Missing copyright notice at top of file
5+
|
6+
1 | # Copyright (C) 2022 , 2023 Ruff
7+
| CPY001
8+
2 |
9+
3 | import os
10+
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_copyright/mod.rs
3+
---
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_copyright/mod.rs
3+
---
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_copyright/mod.rs
3+
---
4+
<filename>:1:1: CPY001 Missing copyright notice at top of file
5+
|
6+
1 | # Copyright (C) 2022- 2023 Ruff
7+
| CPY001
8+
2 |
9+
3 | import os
10+
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_copyright/mod.rs
3+
---
4+
<filename>:1:1: CPY001 Missing copyright notice at top of file
5+
|
6+
1 | # Copyright (C) 2022 - 2023 Ruff
7+
| CPY001
8+
2 |
9+
3 | import os
10+
|

crates/ruff_workspace/src/options.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1131,15 +1131,15 @@ impl Flake8ComprehensionsOptions {
11311131
pub struct Flake8CopyrightOptions {
11321132
/// The regular expression used to match the copyright notice, compiled
11331133
/// with the [`regex`](https://docs.rs/regex/latest/regex/) crate.
1134-
///
1135-
/// Defaults to `(?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}(-\d{4})*`, which matches
1134+
/// Defaults to `(?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}((-|,\s)\d{4})*`, which matches
11361135
/// the following:
11371136
/// - `Copyright 2023`
11381137
/// - `Copyright (C) 2023`
11391138
/// - `Copyright 2021-2023`
11401139
/// - `Copyright (C) 2021-2023`
1140+
/// - `Copyright (C) 2021, 2023`
11411141
#[option(
1142-
default = r#"(?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}([-,]\d{4})*"#,
1142+
default = r#"(?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}((-|,\s)\d{4})*"#,
11431143
value_type = "str",
11441144
example = r#"notice-rgx = "(?i)Copyright \\(C\\) \\d{4}""#
11451145
)]

ruff.schema.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)