Skip to content

Commit

Permalink
fix/issue#11243: allow 3-digit-grouped binary in non_octal_unix_permi…
Browse files Browse the repository at this point in the history
…ssions
  • Loading branch information
cocodery committed Dec 29, 2023
1 parent 174a0d7 commit ca40b0c
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions clippy_lints/src/non_octal_unix_permissions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::numeric_literal::{NumericLiteral, Radix};
use clippy_utils::source::{snippet_opt, snippet_with_applicability};
use clippy_utils::{match_def_path, paths};
use rustc_ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -39,6 +41,24 @@ declare_clippy_lint! {

declare_lint_pass!(NonOctalUnixPermissions => [NON_OCTAL_UNIX_PERMISSIONS]);

fn check_binary_unix_permissions(lit_kind: &LitKind, snip: &str) -> bool {
// support binary unix permissions
if let Some(num_lit) = NumericLiteral::from_lit_kind(snip, lit_kind) {
if num_lit.radix != Radix::Binary {
return false;
}

let group_sizes: Vec<usize> = num_lit.integer.split('_').map(str::len).collect();
// check whether is binary format unix permissions
if group_sizes.len() != 3 {
return false;
}
group_sizes.iter().all(|len| *len == 3)
} else {
false
}
}

impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
match &expr.kind {
Expand All @@ -51,26 +71,22 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
))
|| (path.ident.name == sym!(set_mode)
&& cx.tcx.is_diagnostic_item(sym::FsPermissions, adt.did())))
&& let ExprKind::Lit(_) = param.kind
&& let ExprKind::Lit(lit_kind) = param.kind
&& param.span.eq_ctxt(expr.span)
&& let Some(snip) = snippet_opt(cx, param.span)
&& !(snip.starts_with("0o") || check_binary_unix_permissions(&lit_kind.node, &snip))
{
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]) => {
if let ExprKind::Path(ref path) = func.kind
&& let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
&& match_def_path(cx, def_id, &paths::PERMISSIONS_FROM_MODE)
&& let ExprKind::Lit(_) = param.kind
&& let ExprKind::Lit(lit_kind) = param.kind
&& param.span.eq_ctxt(expr.span)
&& let Some(snip) = snippet_opt(cx, param.span)
&& !snip.starts_with("0o")
&& !(snip.starts_with("0o") || check_binary_unix_permissions(&lit_kind.node, &snip))
{
show_error(cx, param);
}
Expand Down

0 comments on commit ca40b0c

Please sign in to comment.