Skip to content

Commit 30e6e85

Browse files
dbosochansuke
dboso
authored andcommitted
add [permissions_set_readonly_false] rust-lang#9702
1 parent b3145fe commit 30e6e85

6 files changed

+92
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4461,6 +4461,7 @@ Released 2018-09-13
44614461
[`partialeq_to_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_to_none
44624462
[`path_buf_push_overwrite`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_buf_push_overwrite
44634463
[`pattern_type_mismatch`]: https://rust-lang.github.io/rust-clippy/master/index.html#pattern_type_mismatch
4464+
[`permissions_set_readonly_false`]: https://rust-lang.github.io/rust-clippy/master/index.html#permissions_set_readonly_false
44644465
[`positional_named_format_parameters`]: https://rust-lang.github.io/rust-clippy/master/index.html#positional_named_format_parameters
44654466
[`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma
44664467
[`precedence`]: https://rust-lang.github.io/rust-clippy/master/index.html#precedence

clippy_lints/src/declared_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
495495
crate::pass_by_ref_or_value::LARGE_TYPES_PASSED_BY_VALUE_INFO,
496496
crate::pass_by_ref_or_value::TRIVIALLY_COPY_PASS_BY_REF_INFO,
497497
crate::pattern_type_mismatch::PATTERN_TYPE_MISMATCH_INFO,
498+
crate::permissions_set_readonly_false::PERMISSIONS_SET_READONLY_FALSE_INFO,
498499
crate::precedence::PRECEDENCE_INFO,
499500
crate::ptr::CMP_NULL_INFO,
500501
crate::ptr::INVALID_NULL_PTR_USAGE_INFO,

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ mod partialeq_ne_impl;
235235
mod partialeq_to_none;
236236
mod pass_by_ref_or_value;
237237
mod pattern_type_mismatch;
238+
mod permissions_set_readonly_false;
238239
mod precedence;
239240
mod ptr;
240241
mod ptr_offset_with_cast;
@@ -904,6 +905,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
904905
store.register_late_pass(move |_| Box::new(manual_is_ascii_check::ManualIsAsciiCheck::new(msrv())));
905906
store.register_late_pass(|_| Box::new(semicolon_block::SemicolonBlock));
906907
store.register_late_pass(|_| Box::new(fn_null_check::FnNullCheck));
908+
store.register_late_pass(|_| Box::new(permissions_set_readonly_false::PermissionsSetReadonlyFalse));
907909
// add lints here, do not remove this comment, it's used in `new_lint`
908910
}
909911

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use clippy_utils::diagnostics::span_lint_and_note;
2+
use clippy_utils::paths;
3+
use clippy_utils::ty::match_type;
4+
use rustc_ast::ast::LitKind;
5+
use rustc_hir::{Expr, ExprKind};
6+
use rustc_lint::{LateContext, LateLintPass};
7+
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
9+
declare_clippy_lint! {
10+
/// ### What it does
11+
/// Checks for calls to `std::fs::Permissions.set_readonly` with argument `false`
12+
///
13+
/// ### Why is this bad?
14+
/// On Unix platforms this results in the file being world writable
15+
///
16+
/// ### Example
17+
/// ```rust
18+
/// use std::fs::File;
19+
/// let f = File::create("foo.txt").unwrap();
20+
/// let metadata = f.metadata().unwrap();
21+
/// let mut permissions = metadata.permissions();
22+
/// permissions.set_readonly(false);
23+
/// ```
24+
#[clippy::version = "1.66.0"]
25+
pub PERMISSIONS_SET_READONLY_FALSE,
26+
suspicious,
27+
"Checks for calls to `std::fs::Permissions.set_readonly` with argument `false`"
28+
}
29+
declare_lint_pass!(PermissionsSetReadonlyFalse => [PERMISSIONS_SET_READONLY_FALSE]);
30+
31+
impl<'tcx> LateLintPass<'tcx> for PermissionsSetReadonlyFalse {
32+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
33+
if let ExprKind::MethodCall(path, receiver, [arg], _) = &expr.kind
34+
&& match_type(cx, cx.typeck_results().expr_ty(receiver), &paths::PERMISSIONS)
35+
&& path.ident.name == sym!(set_readonly)
36+
&& let ExprKind::Lit(lit) = &arg.kind
37+
&& LitKind::Bool(false) == lit.node {
38+
span_lint_and_note(
39+
cx,
40+
PERMISSIONS_SET_READONLY_FALSE,
41+
expr.span,
42+
"call to `set_readonly` with argument `false`",
43+
None,
44+
"on Unix platforms this results in the file being world writable",
45+
);
46+
}
47+
}
48+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![allow(unused)]
2+
#![warn(clippy::permissions_set_readonly_false)]
3+
4+
use std::fs::File;
5+
6+
struct A;
7+
8+
impl A {
9+
pub fn set_readonly(&mut self, b: bool) {}
10+
}
11+
12+
fn set_readonly(b: bool) {}
13+
14+
fn main() {
15+
let f = File::create("foo.txt").unwrap();
16+
let metadata = f.metadata().unwrap();
17+
let mut permissions = metadata.permissions();
18+
// lint here
19+
permissions.set_readonly(false);
20+
// no lint
21+
permissions.set_readonly(true);
22+
23+
let mut a = A;
24+
// no lint here - a is not of type std::fs::Permissions
25+
a.set_readonly(false);
26+
27+
// no lint here - plain function
28+
set_readonly(false);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: call to `set_readonly` with argument `false`
2+
--> $DIR/permissions_set_readonly_false.rs:19:5
3+
|
4+
LL | permissions.set_readonly(false);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: on Unix platforms this results in the file being world writable
8+
= note: `-D clippy::permissions-set-readonly-false` implied by `-D warnings`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)