Skip to content

Commit f1f21f7

Browse files
11happyntBre
andauthored
add info for non_octal permissions (#22972)
## Summary <!-- What's the purpose of the change? What does it do, and why? --> Part of #17203, comment: #18541 (comment) ## Test Plan <!-- How was it tested? --> updated snapshot --------- Signed-off-by: Bhuminjay <bhuminjaysoni@gmail.com> Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
1 parent a666f3f commit f1f21f7

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

crates/ruff_linter/src/rules/ruff/rules/non_octal_permissions.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ pub(crate) fn non_octal_permissions(checker: &Checker, call: &ExprCall) {
113113

114114
let mut diagnostic = checker.report_diagnostic(NonOctalPermissions, mode_arg.range());
115115

116+
let Some(mode) = int.as_u16() else {
117+
return;
118+
};
119+
120+
diagnostic.info(format_args!(
121+
"Current value of {mode_literal} ({:#05o}) sets permissions: {}",
122+
mode & 0o7777,
123+
get_permissions(mode)
124+
));
116125
// Don't suggest a fix for 0x or 0b literals.
117126
if mode_literal.starts_with("0x") || mode_literal.starts_with("0b") {
118127
return;
@@ -128,7 +137,10 @@ pub(crate) fn non_octal_permissions(checker: &Checker, call: &ExprCall) {
128137
let Some(suggested) = int.as_u16().and_then(suggest_fix) else {
129138
return;
130139
};
131-
140+
let suggested_permissions = get_permissions(suggested);
141+
diagnostic.info(format_args!(
142+
"Suggested value of {suggested:#05o} sets permissions: {suggested_permissions}"
143+
));
132144
let edit = Edit::range_replacement(format!("{suggested:#o}"), mode_arg.range());
133145
diagnostic.set_fix(Fix::unsafe_edit(edit));
134146
}
@@ -230,3 +242,20 @@ fn suggest_fix(mode: u16) -> Option<u16> {
230242
_ => None,
231243
}
232244
}
245+
246+
fn get_permissions(mode: u16) -> String {
247+
let perm = mode & 0o777;
248+
let mut out = String::with_capacity(9);
249+
let append_permission = |out: &mut String, bits: u16| {
250+
out.push(if bits & 0o4 != 0 { 'r' } else { '-' });
251+
out.push(if bits & 0o2 != 0 { 'w' } else { '-' });
252+
out.push(if bits & 0o1 != 0 { 'x' } else { '-' });
253+
};
254+
out.push_str("u=");
255+
append_permission(&mut out, (perm >> 6) & 0o7);
256+
out.push_str(", g=");
257+
append_permission(&mut out, (perm >> 3) & 0o7);
258+
out.push_str(", o=");
259+
append_permission(&mut out, perm & 0o7);
260+
out
261+
}

crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF064_RUF064.py.snap

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ RUF064 [*] Non-octal mode
1212
8 | os.chmod("foo", 7777) # Error
1313
|
1414
help: Replace with octal literal
15+
info: Current value of 444 (0o674) sets permissions: u=rw-, g=rwx, o=r--
16+
info: Suggested value of 0o444 sets permissions: u=r--, g=r--, o=r--
1517
3 | import os
1618
4 | from pathlib import Path
1719
5 |
@@ -33,6 +35,7 @@ RUF064 Non-octal mode
3335
10 | os.chmod("foo", 99999) # Error
3436
|
3537
help: Replace with octal literal
38+
info: Current value of 7777 (0o7141) sets permissions: u=--x, g=r--, o=--x
3639

3740
RUF064 Non-octal mode
3841
--> RUF064.py:9:17
@@ -44,6 +47,7 @@ RUF064 Non-octal mode
4447
10 | os.chmod("foo", 99999) # Error
4548
|
4649
help: Replace with octal literal
50+
info: Current value of 10000 (0o3420) sets permissions: u=r--, g=-w-, o=---
4751

4852
RUF064 Non-octal mode
4953
--> RUF064.py:10:17
@@ -67,6 +71,8 @@ RUF064 [*] Non-octal mode
6771
13 | os.umask(0o777) # OK
6872
|
6973
help: Replace with octal literal
74+
info: Current value of 777 (0o1411) sets permissions: u=r--, g=--x, o=--x
75+
info: Suggested value of 0o777 sets permissions: u=rwx, g=rwx, o=rwx
7076
9 | os.chmod("foo", 10000) # Error
7177
10 | os.chmod("foo", 99999) # Error
7278
11 |
@@ -87,6 +93,8 @@ RUF064 [*] Non-octal mode
8793
16 | os.fchmod(0, 0o400) # OK
8894
|
8995
help: Replace with octal literal
96+
info: Current value of 400 (0o620) sets permissions: u=rw-, g=-w-, o=---
97+
info: Suggested value of 0o400 sets permissions: u=r--, g=---, o=---
9098
12 | os.umask(777) # Error
9199
13 | os.umask(0o777) # OK
92100
14 |
@@ -107,6 +115,8 @@ RUF064 [*] Non-octal mode
107115
19 | os.lchmod("foo", 0o755) # OK
108116
|
109117
help: Replace with octal literal
118+
info: Current value of 755 (0o1363) sets permissions: u=-wx, g=rw-, o=-wx
119+
info: Suggested value of 0o755 sets permissions: u=rwx, g=r-x, o=r-x
110120
15 | os.fchmod(0, 400) # Error
111121
16 | os.fchmod(0, 0o400) # OK
112122
17 |
@@ -127,6 +137,8 @@ RUF064 [*] Non-octal mode
127137
22 | os.mkdir("foo", 0o600) # OK
128138
|
129139
help: Replace with octal literal
140+
info: Current value of 600 (0o1130) sets permissions: u=--x, g=-wx, o=---
141+
info: Suggested value of 0o600 sets permissions: u=rw-, g=---, o=---
130142
18 | os.lchmod("foo", 755) # Error
131143
19 | os.lchmod("foo", 0o755) # OK
132144
20 |
@@ -147,6 +159,8 @@ RUF064 [*] Non-octal mode
147159
25 | os.makedirs("foo", 0o644) # OK
148160
|
149161
help: Replace with octal literal
162+
info: Current value of 644 (0o1204) sets permissions: u=-w-, g=---, o=r--
163+
info: Suggested value of 0o644 sets permissions: u=rw-, g=r--, o=r--
150164
21 | os.mkdir("foo", 600) # Error
151165
22 | os.mkdir("foo", 0o600) # OK
152166
23 |
@@ -167,6 +181,8 @@ RUF064 [*] Non-octal mode
167181
28 | os.mkfifo("foo", 0o640) # OK
168182
|
169183
help: Replace with octal literal
184+
info: Current value of 640 (0o1200) sets permissions: u=-w-, g=---, o=---
185+
info: Suggested value of 0o640 sets permissions: u=rw-, g=r--, o=---
170186
24 | os.makedirs("foo", 644) # Error
171187
25 | os.makedirs("foo", 0o644) # OK
172188
26 |
@@ -187,6 +203,8 @@ RUF064 [*] Non-octal mode
187203
31 | os.mknod("foo", 0o660) # OK
188204
|
189205
help: Replace with octal literal
206+
info: Current value of 660 (0o1224) sets permissions: u=-w-, g=-w-, o=r--
207+
info: Suggested value of 0o660 sets permissions: u=rw-, g=rw-, o=---
190208
27 | os.mkfifo("foo", 640) # Error
191209
28 | os.mkfifo("foo", 0o640) # OK
192210
29 |
@@ -207,6 +225,8 @@ RUF064 [*] Non-octal mode
207225
34 | os.open("foo", os.O_CREAT, 0o644) # OK
208226
|
209227
help: Replace with octal literal
228+
info: Current value of 644 (0o1204) sets permissions: u=-w-, g=---, o=r--
229+
info: Suggested value of 0o644 sets permissions: u=rw-, g=r--, o=r--
210230
30 | os.mknod("foo", 660) # Error
211231
31 | os.mknod("foo", 0o660) # OK
212232
32 |
@@ -227,6 +247,8 @@ RUF064 [*] Non-octal mode
227247
37 | Path("bar").chmod(0o755) # OK
228248
|
229249
help: Replace with octal literal
250+
info: Current value of 755 (0o1363) sets permissions: u=-wx, g=rw-, o=-wx
251+
info: Suggested value of 0o755 sets permissions: u=rwx, g=r-x, o=r-x
230252
33 | os.open("foo", os.O_CREAT, 644) # Error
231253
34 | os.open("foo", os.O_CREAT, 0o644) # OK
232254
35 |
@@ -246,6 +268,8 @@ RUF064 [*] Non-octal mode
246268
41 | path.chmod(0o755) # OK
247269
|
248270
help: Replace with octal literal
271+
info: Current value of 755 (0o1363) sets permissions: u=-wx, g=rw-, o=-wx
272+
info: Suggested value of 0o755 sets permissions: u=rwx, g=r-x, o=r-x
249273
37 | Path("bar").chmod(0o755) # OK
250274
38 |
251275
39 | path = Path("bar")
@@ -266,6 +290,8 @@ RUF064 [*] Non-octal mode
266290
44 | dbm.open("db", "r", 0o600) # OK
267291
|
268292
help: Replace with octal literal
293+
info: Current value of 600 (0o1130) sets permissions: u=--x, g=-wx, o=---
294+
info: Suggested value of 0o600 sets permissions: u=rw-, g=---, o=---
269295
40 | path.chmod(755) # Error
270296
41 | path.chmod(0o755) # OK
271297
42 |
@@ -286,6 +312,8 @@ RUF064 [*] Non-octal mode
286312
47 | dbm.gnu.open("db", "r", 0o600) # OK
287313
|
288314
help: Replace with octal literal
315+
info: Current value of 600 (0o1130) sets permissions: u=--x, g=-wx, o=---
316+
info: Suggested value of 0o600 sets permissions: u=rw-, g=---, o=---
289317
43 | dbm.open("db", "r", 600) # Error
290318
44 | dbm.open("db", "r", 0o600) # OK
291319
45 |
@@ -306,6 +334,8 @@ RUF064 [*] Non-octal mode
306334
50 | dbm.ndbm.open("db", "r", 0o600) # OK
307335
|
308336
help: Replace with octal literal
337+
info: Current value of 600 (0o1130) sets permissions: u=--x, g=-wx, o=---
338+
info: Suggested value of 0o600 sets permissions: u=rw-, g=---, o=---
309339
46 | dbm.gnu.open("db", "r", 600) # Error
310340
47 | dbm.gnu.open("db", "r", 0o600) # OK
311341
48 |
@@ -326,6 +356,8 @@ RUF064 [*] Non-octal mode
326356
53 | os.fchmod(0, 493) # 0o755
327357
|
328358
help: Replace with octal literal
359+
info: Current value of 256 (0o400) sets permissions: u=r--, g=---, o=---
360+
info: Suggested value of 0o400 sets permissions: u=r--, g=---, o=---
329361
49 | dbm.ndbm.open("db", "r", 600) # Error
330362
50 | dbm.ndbm.open("db", "r", 0o600) # OK
331363
51 |
@@ -346,6 +378,8 @@ RUF064 [*] Non-octal mode
346378
55 | # https://github.com/astral-sh/ruff/issues/19010
347379
|
348380
help: Replace with octal literal
381+
info: Current value of 493 (0o755) sets permissions: u=rwx, g=r-x, o=r-x
382+
info: Suggested value of 0o755 sets permissions: u=rwx, g=r-x, o=r-x
349383
50 | dbm.ndbm.open("db", "r", 0o600) # OK
350384
51 |
351385
52 | os.fchmod(0, 256) # 0o400
@@ -365,6 +399,7 @@ RUF064 [*] Non-octal mode
365399
57 | os.chmod("foo", 0000) # Error
366400
|
367401
help: Replace with octal literal
402+
info: Current value of 000 (0o000) sets permissions: u=---, g=---, o=---
368403
53 | os.fchmod(0, 493) # 0o755
369404
54 |
370405
55 | # https://github.com/astral-sh/ruff/issues/19010
@@ -385,6 +420,7 @@ RUF064 [*] Non-octal mode
385420
59 | os.chmod("foo", 0b0) # Error
386421
|
387422
help: Replace with octal literal
423+
info: Current value of 0000 (0o000) sets permissions: u=---, g=---, o=---
388424
54 |
389425
55 | # https://github.com/astral-sh/ruff/issues/19010
390426
56 | os.chmod("foo", 000) # Error
@@ -405,6 +441,7 @@ RUF064 Non-octal mode
405441
61 | os.chmod("foo", 0) # Ok
406442
|
407443
help: Replace with octal literal
444+
info: Current value of 0b0 (0o000) sets permissions: u=---, g=---, o=---
408445

409446
RUF064 Non-octal mode
410447
--> RUF064.py:60:17
@@ -415,3 +452,4 @@ RUF064 Non-octal mode
415452
61 | os.chmod("foo", 0) # Ok
416453
|
417454
help: Replace with octal literal
455+
info: Current value of 0x0 (0o000) sets permissions: u=---, g=---, o=---

0 commit comments

Comments
 (0)