Skip to content

Commit 3b8323d

Browse files
committed
Auto merge of #12049 - cocodery:fix/issue#11243, r=Alexendoo
fix/issue#11243: allow 3-digit-grouped binary in non_octal_unix_permissions fixes [Issue#11243](#11243) Issue#11243 suggest lint `non_octal_unix_permissions` should not report binary format literal unix permissions as an error, and we think binary format is a good way to understand these permissions. To solve this problem, we need to add check for binary literal, which is written in function `check_binary_unix_permissions` , only `binary, 3 groups and each group length equals to 3` is a legal format. changelog: [`non_octal_unix_permissions`]: Add check for binary format literal unix permissions like 0b111_111_111
2 parents 7fbaba8 + a843996 commit 3b8323d

4 files changed

+13
-9
lines changed

clippy_lints/src/non_octal_unix_permissions.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,10 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
5353
&& cx.tcx.is_diagnostic_item(sym::FsPermissions, adt.did())))
5454
&& let ExprKind::Lit(_) = param.kind
5555
&& param.span.eq_ctxt(expr.span)
56+
&& let Some(snip) = snippet_opt(cx, param.span)
57+
&& !(snip.starts_with("0o") || snip.starts_with("0b"))
5658
{
57-
let Some(snip) = snippet_opt(cx, param.span) else {
58-
return;
59-
};
60-
61-
if !snip.starts_with("0o") {
62-
show_error(cx, param);
63-
}
59+
show_error(cx, param);
6460
}
6561
},
6662
ExprKind::Call(func, [param]) => {
@@ -70,7 +66,7 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
7066
&& let ExprKind::Lit(_) = param.kind
7167
&& param.span.eq_ctxt(expr.span)
7268
&& let Some(snip) = snippet_opt(cx, param.span)
73-
&& !snip.starts_with("0o")
69+
&& !(snip.starts_with("0o") || snip.starts_with("0b"))
7470
{
7571
show_error(cx, param);
7672
}

tests/ui/non_octal_unix_permissions.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ fn main() {
2525

2626
permissions.set_mode(0o644);
2727
permissions.set_mode(0o704);
28+
// no error
29+
permissions.set_mode(0b111_000_100);
2830

2931
// DirBuilderExt::mode
3032
let mut builder = DirBuilder::new();
3133
builder.mode(0o755);
3234
builder.mode(0o406);
35+
// no error
36+
permissions.set_mode(0b111000100);
3337
}

tests/ui/non_octal_unix_permissions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ fn main() {
2525

2626
permissions.set_mode(644);
2727
permissions.set_mode(0o704);
28+
// no error
29+
permissions.set_mode(0b111_000_100);
2830

2931
// DirBuilderExt::mode
3032
let mut builder = DirBuilder::new();
3133
builder.mode(755);
3234
builder.mode(0o406);
35+
// no error
36+
permissions.set_mode(0b111000100);
3337
}

tests/ui/non_octal_unix_permissions.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | permissions.set_mode(644);
2020
| ^^^ help: consider using an octal literal instead: `0o644`
2121

2222
error: using a non-octal value to set unix file permissions
23-
--> $DIR/non_octal_unix_permissions.rs:31:18
23+
--> $DIR/non_octal_unix_permissions.rs:33:18
2424
|
2525
LL | builder.mode(755);
2626
| ^^^ help: consider using an octal literal instead: `0o755`

0 commit comments

Comments
 (0)