Skip to content

Commit a843996

Browse files
committed
Simplify check function, weirdly binary format literals happen not often
1 parent 60c647b commit a843996

File tree

1 file changed

+4
-29
lines changed

1 file changed

+4
-29
lines changed

clippy_lints/src/non_octal_unix_permissions.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::numeric_literal::{NumericLiteral, Radix};
32
use clippy_utils::source::{snippet_opt, snippet_with_applicability};
43
use clippy_utils::{match_def_path, paths};
5-
use rustc_ast::LitKind;
64
use rustc_errors::Applicability;
75
use rustc_hir::{Expr, ExprKind};
86
use rustc_lint::{LateContext, LateLintPass};
@@ -41,29 +39,6 @@ declare_clippy_lint! {
4139

4240
declare_lint_pass!(NonOctalUnixPermissions => [NON_OCTAL_UNIX_PERMISSIONS]);
4341

44-
fn check_binary_unix_permissions(lit_kind: &LitKind, snip: &str) -> bool {
45-
// support binary unix permissions
46-
if let Some(num_lit) = NumericLiteral::from_lit_kind(snip, lit_kind) {
47-
if num_lit.radix != Radix::Binary {
48-
return false;
49-
}
50-
51-
let group_sizes: Vec<usize> = num_lit.integer.split('_').map(str::len).collect();
52-
// check whether is binary format unix permissions
53-
if group_sizes.len() == 1 && (num_lit.integer.len() == 9 || num_lit.integer.len() == 12) {
54-
// 0bxxxxxxxxx or 0bxxxxxxxxxxxx
55-
true
56-
} else if group_sizes.len() == 3 || group_sizes.len() == 4 {
57-
// 0bxxx_xxx_xxx or 0bxxx_xxx_xxx_xxx
58-
group_sizes.iter().all(|len| *len == 3)
59-
} else {
60-
false
61-
}
62-
} else {
63-
false
64-
}
65-
}
66-
6742
impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
6843
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
6944
match &expr.kind {
@@ -76,10 +51,10 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
7651
))
7752
|| (path.ident.name == sym!(set_mode)
7853
&& cx.tcx.is_diagnostic_item(sym::FsPermissions, adt.did())))
79-
&& let ExprKind::Lit(lit_kind) = param.kind
54+
&& let ExprKind::Lit(_) = param.kind
8055
&& param.span.eq_ctxt(expr.span)
8156
&& let Some(snip) = snippet_opt(cx, param.span)
82-
&& !(snip.starts_with("0o") || check_binary_unix_permissions(&lit_kind.node, &snip))
57+
&& !(snip.starts_with("0o") || snip.starts_with("0b"))
8358
{
8459
show_error(cx, param);
8560
}
@@ -88,10 +63,10 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
8863
if let ExprKind::Path(ref path) = func.kind
8964
&& let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
9065
&& match_def_path(cx, def_id, &paths::PERMISSIONS_FROM_MODE)
91-
&& let ExprKind::Lit(lit_kind) = param.kind
66+
&& let ExprKind::Lit(_) = param.kind
9267
&& param.span.eq_ctxt(expr.span)
9368
&& let Some(snip) = snippet_opt(cx, param.span)
94-
&& !(snip.starts_with("0o") || check_binary_unix_permissions(&lit_kind.node, &snip))
69+
&& !(snip.starts_with("0o") || snip.starts_with("0b"))
9570
{
9671
show_error(cx, param);
9772
}

0 commit comments

Comments
 (0)