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](https://github.com/rust-lang/rust-clippy/issues/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
This commit is contained in:
bors 2024-01-08 19:09:42 +00:00
commit 3b8323d790
4 changed files with 13 additions and 9 deletions

View File

@ -53,14 +53,10 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
&& cx.tcx.is_diagnostic_item(sym::FsPermissions, adt.did())))
&& let ExprKind::Lit(_) = param.kind
&& param.span.eq_ctxt(expr.span)
&& let Some(snip) = snippet_opt(cx, param.span)
&& !(snip.starts_with("0o") || snip.starts_with("0b"))
{
let Some(snip) = snippet_opt(cx, param.span) else {
return;
};
if !snip.starts_with("0o") {
show_error(cx, param);
}
show_error(cx, param);
}
},
ExprKind::Call(func, [param]) => {
@ -70,7 +66,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
&& let ExprKind::Lit(_) = param.kind
&& param.span.eq_ctxt(expr.span)
&& let Some(snip) = snippet_opt(cx, param.span)
&& !snip.starts_with("0o")
&& !(snip.starts_with("0o") || snip.starts_with("0b"))
{
show_error(cx, param);
}

View File

@ -25,9 +25,13 @@ fn main() {
permissions.set_mode(0o644);
permissions.set_mode(0o704);
// no error
permissions.set_mode(0b111_000_100);
// DirBuilderExt::mode
let mut builder = DirBuilder::new();
builder.mode(0o755);
builder.mode(0o406);
// no error
permissions.set_mode(0b111000100);
}

View File

@ -25,9 +25,13 @@ fn main() {
permissions.set_mode(644);
permissions.set_mode(0o704);
// no error
permissions.set_mode(0b111_000_100);
// DirBuilderExt::mode
let mut builder = DirBuilder::new();
builder.mode(755);
builder.mode(0o406);
// no error
permissions.set_mode(0b111000100);
}

View File

@ -20,7 +20,7 @@ LL | permissions.set_mode(644);
| ^^^ help: consider using an octal literal instead: `0o644`
error: using a non-octal value to set unix file permissions
--> $DIR/non_octal_unix_permissions.rs:31:18
--> $DIR/non_octal_unix_permissions.rs:33:18
|
LL | builder.mode(755);
| ^^^ help: consider using an octal literal instead: `0o755`