Skip to content

Commit

Permalink
Remove FIXME and make it retain old raw string hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jul 3, 2023
1 parent 3e33a75 commit f0e740f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
21 changes: 14 additions & 7 deletions clippy_lints/src/bare_dos_device_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use clippy_utils::{
diagnostics::span_lint_and_then, get_parent_expr, is_from_proc_macro, match_def_path, path_res, paths::PATH_NEW,
ty::is_type_diagnostic_item,
};
use rustc_ast::LitKind;
use rustc_ast::{LitKind, StrStyle};
use rustc_errors::Applicability;
use rustc_hir::def_id::DefId;
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::{lint::in_external_macro, ty};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{sym, Symbol};
use std::borrow::Cow;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -41,7 +42,7 @@ impl<'tcx> LateLintPass<'tcx> for BareDosDeviceNames {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if !in_external_macro(cx.sess(), expr.span)
&& let ExprKind::Lit(arg) = expr.kind
&& let LitKind::Str(str_sym, _) = arg.node
&& let LitKind::Str(str_sym, str_style) = arg.node
&& matches!(
&*str_sym.as_str().to_ascii_lowercase(),
"aux"
Expand Down Expand Up @@ -86,20 +87,26 @@ impl<'tcx> LateLintPass<'tcx> for BareDosDeviceNames {
expr.span,
"this path refers to a DOS device",
|diag| {
// Keep `r###` and `###`
let (prefix, hashes) = if let StrStyle::Raw(num) = str_style {
(Cow::Borrowed("r"), "#".repeat(num as usize).into())
} else {
(Cow::Borrowed(""), Cow::Borrowed(""))
};

// Suggest making current behavior explicit
diag.span_suggestion_verbose(
expr.span,
"if this is intended, try",
// FIXME: I have zero clue why it normalizes this. `\` -> `/`
format!(r#"r"\\.\{str_sym}"\"#),
"if this is intended, use",
format!(r#"r{hashes}"\\.\{str_sym}"{hashes}"#),
Applicability::MaybeIncorrect,
);

// Suggest making the code refer to a file or folder in the current directory
diag.span_suggestion_verbose(
expr.span,
"if this was intended to point to a file or folder, try",
format!("\"./{str_sym}\""),
"if this was intended to point to a file or folder, use",
format!(r#"{prefix}{hashes}"./{str_sym}"{hashes}"#),
Applicability::MaybeIncorrect,
);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/bare_dos_device_names.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@aux-build:proc_macros.rs:proc-macro
#![allow(clippy::no_effect, unused)]
#![allow(clippy::needless_raw_string_hashes, clippy::no_effect, unused)]
#![warn(clippy::bare_dos_device_names)]

#[macro_use]
Expand All @@ -20,6 +20,9 @@ fn main() {
b("conin$");
File::open("conin$");
std::path::PathBuf::from("a");
// Keep raw string
Path::new(r##"aux"##);
// Don't lint
PathBuf::from("a");
Path::new("a");
external! {
Expand Down
41 changes: 28 additions & 13 deletions tests/ui/bare_dos_device_names.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ LL | a("con");
| ^^^^^
|
= note: `-D clippy::bare-dos-device-names` implied by `-D warnings`
help: if this is intended, try
help: if this is intended, use
|
LL | a(r"//./con"/);
| ~~~~~~~~~~~
help: if this was intended to point to a file or folder, try
LL | a(r"//./con");
| ~~~~~~~~~~
help: if this was intended to point to a file or folder, use
|
LL | a("./con");
| ~~~~~~~
Expand All @@ -20,11 +20,11 @@ error: this path refers to a DOS device
LL | b("conin$");
| ^^^^^^^^
|
help: if this is intended, try
help: if this is intended, use
|
LL | b(r"//./conin$"/);
| ~~~~~~~~~~~~~~
help: if this was intended to point to a file or folder, try
LL | b(r"//./conin$");
| ~~~~~~~~~~~~~
help: if this was intended to point to a file or folder, use
|
LL | b("./conin$");
| ~~~~~~~~~~
Expand All @@ -35,14 +35,29 @@ error: this path refers to a DOS device
LL | File::open("conin$");
| ^^^^^^^^
|
help: if this is intended, try
help: if this is intended, use
|
LL | File::open(r"//./conin$"/);
| ~~~~~~~~~~~~~~
help: if this was intended to point to a file or folder, try
LL | File::open(r"//./conin$");
| ~~~~~~~~~~~~~
help: if this was intended to point to a file or folder, use
|
LL | File::open("./conin$");
| ~~~~~~~~~~

error: aborting due to 3 previous errors
error: this path refers to a DOS device
--> $DIR/bare_dos_device_names.rs:24:15
|
LL | Path::new(r##"aux"##);
| ^^^^^^^^^^
|
help: if this is intended, use
|
LL | Path::new(r##"//./aux"##);
| ~~~~~~~~~~~~~~
help: if this was intended to point to a file or folder, use
|
LL | Path::new(r##"./aux"##);
| ~~~~~~~~~~~~

error: aborting due to 4 previous errors

0 comments on commit f0e740f

Please # to comment.