fix false positive in suspicious_open_options, make paths work
This commit is contained in:
parent
515fe65ba8
commit
f09cd88199
6 changed files with 87 additions and 27 deletions
|
|
@ -2833,10 +2833,11 @@ declare_clippy_lint! {
|
|||
/// without an explicit `OpenOptions::truncate()`.
|
||||
///
|
||||
/// ### Why is this bad?
|
||||
/// create() alone will either create a new file or open an
|
||||
/// `create()` alone will either create a new file or open an
|
||||
/// existing file. If the file already exists, it will be
|
||||
/// overwritten when written to, but the file will not be
|
||||
/// truncated by default. If less data is written to the file
|
||||
/// truncated by default.
|
||||
/// If less data is written to the file
|
||||
/// than it already contains, the remainder of the file will
|
||||
/// remain unchanged, and the end of the file will contain old
|
||||
/// data.
|
||||
|
|
@ -2847,10 +2848,14 @@ declare_clippy_lint! {
|
|||
/// `truncate(false)` will explicitely keep the default behavior.
|
||||
///
|
||||
/// ### Example
|
||||
/// ```rust
|
||||
/// ```rust,no_run
|
||||
/// use std::fs::OpenOptions;
|
||||
///
|
||||
/// OpenOptions::new().create(true);
|
||||
/// ```
|
||||
/// Use instead:
|
||||
/// ```rust,no_run
|
||||
/// use std::fs::OpenOptions;
|
||||
///
|
||||
/// OpenOptions::new().create(true).truncate(true);
|
||||
/// ```
|
||||
|
|
|
|||
|
|
@ -19,11 +19,12 @@ fn is_open_options(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
|
|||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
|
||||
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
|
||||
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
|
||||
//&& is_open_options(cx, cx.tcx.type_of(impl_id).instantiate_identity())
|
||||
&& is_open_options(cx, cx.tcx.type_of(impl_id).instantiate_identity())
|
||||
{
|
||||
let mut options = Vec::new();
|
||||
get_open_options(cx, recv, &mut options);
|
||||
check_open_options(cx, &options, e.span);
|
||||
if get_open_options(cx, recv, &mut options) {
|
||||
check_open_options(cx, &options, e.span);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -55,6 +56,9 @@ impl std::fmt::Display for OpenOption {
|
|||
}
|
||||
}
|
||||
|
||||
/// Collects information about a method call chain on `OpenOptions`.
|
||||
/// Returns false if an unexpected expression kind was found "on the way",
|
||||
/// and linting should then be avoided.
|
||||
fn get_open_options(
|
||||
cx: &LateContext<'_>,
|
||||
argument: &Expr<'_>,
|
||||
|
|
@ -102,7 +106,16 @@ fn get_open_options(
|
|||
"write" => {
|
||||
options.push((OpenOption::Write, argument_option, span));
|
||||
},
|
||||
_ => (),
|
||||
_ => {
|
||||
// Avoid linting altogether if this method is from a trait.
|
||||
// This might be a user defined extension trait with a method like `truncate_write`
|
||||
// which would be a false positive
|
||||
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(argument.hir_id)
|
||||
&& cx.tcx.trait_of_item(method_def_id).is_some()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
get_open_options(cx, receiver, options)
|
||||
|
|
@ -113,9 +126,17 @@ fn get_open_options(
|
|||
&& let ExprKind::Path(path) = callee.kind
|
||||
&& let Some(did) = cx.qpath_res(&path, callee.hir_id).opt_def_id()
|
||||
{
|
||||
match_any_def_paths(cx, did, &[&paths::TOKIO_IO_OPEN_OPTIONS]).is_some()
|
||||
//is_type_diagnostic_item(cx, ty, sym::FsOpenOptions) || match_type(cx, ty,
|
||||
// &paths::TOKIO_IO_OPEN_OPTIONS)
|
||||
match_any_def_paths(
|
||||
cx,
|
||||
did,
|
||||
&[
|
||||
&paths::TOKIO_IO_OPEN_OPTIONS_NEW,
|
||||
&paths::OPEN_OPTIONS_NEW,
|
||||
&paths::FILE_OPTIONS,
|
||||
&paths::TOKIO_FILE_OPTIONS,
|
||||
],
|
||||
)
|
||||
.is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
|
@ -168,9 +189,17 @@ fn check_open_options(cx: &LateContext<'_>, settings: &[(OpenOption, Argument, S
|
|||
*create_span,
|
||||
"file opened with `create`, but `truncate` behavior not defined",
|
||||
|diag| {
|
||||
diag
|
||||
.span_suggestion(create_span.shrink_to_hi(), "add", ".truncate(true)".to_string(), rustc_errors::Applicability::MaybeIncorrect)
|
||||
.help("if you intend to overwrite an existing file entirely, call `.truncate(true)`. if you instead know that you may want to keep some parts of the old file, call `.truncate(false)`. Alternatively, use `.append` to append to the file instead of overwriting it.");
|
||||
diag.span_suggestion(
|
||||
create_span.shrink_to_hi(),
|
||||
"add",
|
||||
".truncate(true)".to_string(),
|
||||
rustc_errors::Applicability::MaybeIncorrect,
|
||||
)
|
||||
.help("if you intend to overwrite an existing file entirely, call `.truncate(true)`")
|
||||
.help(
|
||||
"if you instead know that you may want to keep some parts of the old file, call `.truncate(false)`",
|
||||
)
|
||||
.help("alternatively, use `.append(true)` to append to the file instead of overwriting it");
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue