-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #10063 - chansuke:issue-9702, r=Alexendoo
add [`permissions_set_readonly_false`] #9702 Add slight modification on [this PR](rust-lang/rust-clippy#9744). --- changelog: New lint [`permissions_set_readonly_false`] [#10063](rust-lang/rust-clippy#10063) <!-- changelog_checked -->
- Loading branch information
Showing
6 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::paths; | ||
use clippy_utils::ty::match_type; | ||
use rustc_ast::ast::LitKind; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for calls to `std::fs::Permissions.set_readonly` with argument `false`. | ||
/// | ||
/// ### Why is this bad? | ||
/// On Unix platforms this results in the file being world writable, | ||
/// equivalent to `chmod a+w <file>`. | ||
/// ### Example | ||
/// ```rust | ||
/// use std::fs::File; | ||
/// let f = File::create("foo.txt").unwrap(); | ||
/// let metadata = f.metadata().unwrap(); | ||
/// let mut permissions = metadata.permissions(); | ||
/// permissions.set_readonly(false); | ||
/// ``` | ||
#[clippy::version = "1.66.0"] | ||
pub PERMISSIONS_SET_READONLY_FALSE, | ||
suspicious, | ||
"Checks for calls to `std::fs::Permissions.set_readonly` with argument `false`" | ||
} | ||
declare_lint_pass!(PermissionsSetReadonlyFalse => [PERMISSIONS_SET_READONLY_FALSE]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for PermissionsSetReadonlyFalse { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
if let ExprKind::MethodCall(path, receiver, [arg], _) = &expr.kind | ||
&& match_type(cx, cx.typeck_results().expr_ty(receiver), &paths::PERMISSIONS) | ||
&& path.ident.name == sym!(set_readonly) | ||
&& let ExprKind::Lit(lit) = &arg.kind | ||
&& LitKind::Bool(false) == lit.node | ||
{ | ||
span_lint_and_then( | ||
cx, | ||
PERMISSIONS_SET_READONLY_FALSE, | ||
expr.span, | ||
"call to `set_readonly` with argument `false`", | ||
|diag| { | ||
diag.note("on Unix platforms this results in the file being world writable"); | ||
diag.help("you can set the desired permissions using `PermissionsExt`. For more information, see\n\ | ||
https://doc.rust-lang.org/std/os/unix/fs/trait.PermissionsExt.html"); | ||
} | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::permissions_set_readonly_false)] | ||
|
||
use std::fs::File; | ||
|
||
struct A; | ||
|
||
impl A { | ||
pub fn set_readonly(&mut self, b: bool) {} | ||
} | ||
|
||
fn set_readonly(b: bool) {} | ||
|
||
fn main() { | ||
let f = File::create("foo.txt").unwrap(); | ||
let metadata = f.metadata().unwrap(); | ||
let mut permissions = metadata.permissions(); | ||
// lint here | ||
permissions.set_readonly(false); | ||
// no lint | ||
permissions.set_readonly(true); | ||
|
||
let mut a = A; | ||
// no lint here - a is not of type std::fs::Permissions | ||
a.set_readonly(false); | ||
|
||
// no lint here - plain function | ||
set_readonly(false); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error: call to `set_readonly` with argument `false` | ||
--> $DIR/permissions_set_readonly_false.rs:19:5 | ||
| | ||
LL | permissions.set_readonly(false); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: on Unix platforms this results in the file being world writable | ||
= help: you can set the desired permissions using `PermissionsExt`. For more information, see | ||
https://doc.rust-lang.org/std/os/unix/fs/trait.PermissionsExt.html | ||
= note: `-D clippy::permissions-set-readonly-false` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|