forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bad_open_mode.rs
197 lines (177 loc) · 5.37 KB
/
bad_open_mode.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use bitflags::bitflags;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Constant, Expr};
use ruff_python_semantic::SemanticModel;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
/// ## What it does
/// Check for an invalid `mode` argument in `open` calls.
///
/// ## Why is this bad?
/// The `open` function accepts a `mode` argument that specifies how the file
/// should be opened (e.g., read-only, write-only, append-only, etc.).
///
/// Python supports a variety of open modes: `r`, `w`, `a`, and `x`, to control
/// reading, writing, appending, and creating, respectively, along with
/// `b` (binary mode), `+` (read and write), and `U` (universal newlines),
/// the latter of which is only valid alongside `r`. This rule detects both
/// invalid combinations of modes and invalid characters in the mode string
/// itself.
///
/// ## Example
/// ```python
/// with open("file", "rwx") as f:
/// return f.read()
/// ```
///
/// Use instead:
/// ```python
/// with open("file", "r") as f:
/// return f.read()
/// ```
///
/// ## References
/// - [Python documentation: `open`](https://docs.python.org/3/library/functions.html#open)
#[violation]
pub struct BadOpenMode {
mode: String,
}
impl Violation for BadOpenMode {
#[derive_message_formats]
fn message(&self) -> String {
let BadOpenMode { mode } = self;
format!("`{mode}` is not a valid mode for `open`")
}
}
/// PLW1501
pub(crate) fn bad_open_mode(checker: &mut Checker, call: &ast::ExprCall) {
let Some(kind) = is_open(call.func.as_ref(), checker.semantic()) else {
return;
};
let Some(mode) = extract_mode(call, kind) else {
return;
};
let Expr::Constant(ast::ExprConstant {
value: Constant::Str(ast::StringConstant { value, .. }),
..
}) = mode
else {
return;
};
if is_valid_mode(value) {
return;
}
checker.diagnostics.push(Diagnostic::new(
BadOpenMode {
mode: value.to_string(),
},
mode.range(),
));
}
#[derive(Debug, Copy, Clone)]
enum Kind {
/// A call to the builtin `open(...)`.
Builtin,
/// A call to `pathlib.Path(...).open(...)`.
Pathlib,
}
/// If a function is a call to `open`, returns the kind of `open` call.
fn is_open(func: &Expr, semantic: &SemanticModel) -> Option<Kind> {
match func {
// Ex) `pathlib.Path(...).open(...)`
Expr::Attribute(ast::ExprAttribute { attr, value, .. }) if attr.as_str() == "open" => {
match value.as_ref() {
Expr::Call(ast::ExprCall { func, .. }) => semantic
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["pathlib", "Path"]))
.then_some(Kind::Pathlib),
_ => None,
}
}
// Ex) `open(...)`
Expr::Name(ast::ExprName { id, .. }) => {
(id.as_str() == "open" && semantic.is_builtin("open")).then_some(Kind::Builtin)
}
_ => None,
}
}
/// Returns the mode argument, if present.
fn extract_mode(call: &ast::ExprCall, kind: Kind) -> Option<&Expr> {
match kind {
Kind::Builtin => call.arguments.find_argument("mode", 1),
Kind::Pathlib => call.arguments.find_argument("mode", 0),
}
}
bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(super) struct OpenMode: u8 {
/// `r`
const READ = 0b0001;
/// `w`
const WRITE = 0b0010;
/// `a`
const APPEND = 0b0100;
/// `x`
const CREATE = 0b1000;
/// `b`
const BINARY = 0b10000;
/// `t`
const TEXT = 0b10_0000;
/// `+`
const PLUS = 0b100_0000;
/// `U`
const UNIVERSAL_NEWLINES = 0b1000_0000;
}
}
impl TryFrom<char> for OpenMode {
type Error = ();
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'r' => Ok(Self::READ),
'w' => Ok(Self::WRITE),
'a' => Ok(Self::APPEND),
'x' => Ok(Self::CREATE),
'b' => Ok(Self::BINARY),
't' => Ok(Self::TEXT),
'+' => Ok(Self::PLUS),
'U' => Ok(Self::UNIVERSAL_NEWLINES),
_ => Err(()),
}
}
}
/// Returns `true` if the open mode is valid.
fn is_valid_mode(mode: &str) -> bool {
// Flag duplicates and invalid characters.
let mut flags = OpenMode::empty();
for char in mode.chars() {
let Ok(flag) = OpenMode::try_from(char) else {
return false;
};
if flags.intersects(flag) {
return false;
}
flags.insert(flag);
}
// Both text and binary mode cannot be set at the same time.
if flags.contains(OpenMode::TEXT | OpenMode::BINARY) {
return false;
}
// The `U` mode is only valid with `r`.
if flags.contains(OpenMode::UNIVERSAL_NEWLINES)
&& flags.intersects(OpenMode::WRITE | OpenMode::APPEND | OpenMode::CREATE)
{
return false;
}
// Otherwise, reading, writing, creating, and appending are mutually exclusive.
[
OpenMode::READ | OpenMode::UNIVERSAL_NEWLINES,
OpenMode::WRITE,
OpenMode::CREATE,
OpenMode::APPEND,
]
.into_iter()
.filter(|flag| flags.intersects(*flag))
.count()
== 1
}