Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 123ffdf

Browse files
committedJul 3, 2023
Remove FIXME and make it retain old raw string hashes
1 parent 3e33a75 commit 123ffdf

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed
 

‎clippy_lints/src/bare_dos_device_names.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use clippy_utils::{
22
diagnostics::span_lint_and_then, get_parent_expr, is_from_proc_macro, match_def_path, path_res, paths::PATH_NEW,
33
ty::is_type_diagnostic_item,
44
};
5-
use rustc_ast::LitKind;
5+
use rustc_ast::{LitKind, StrStyle};
66
use rustc_errors::Applicability;
77
use rustc_hir::def_id::DefId;
88
use rustc_hir::{Expr, ExprKind, QPath};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
1010
use rustc_middle::{lint::in_external_macro, ty};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
1212
use rustc_span::{sym, Symbol};
13+
use std::borrow::Cow;
1314

1415
declare_clippy_lint! {
1516
/// ### What it does
@@ -38,10 +39,11 @@ declare_clippy_lint! {
3839
declare_lint_pass!(BareDosDeviceNames => [BARE_DOS_DEVICE_NAMES]);
3940

4041
impl<'tcx> LateLintPass<'tcx> for BareDosDeviceNames {
42+
#[expect(clippy::cast_possible_truncation)]
4143
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
4244
if !in_external_macro(cx.sess(), expr.span)
4345
&& let ExprKind::Lit(arg) = expr.kind
44-
&& let LitKind::Str(str_sym, _) = arg.node
46+
&& let LitKind::Str(str_sym, str_style) = arg.node
4547
&& matches!(
4648
&*str_sym.as_str().to_ascii_lowercase(),
4749
"aux"
@@ -86,20 +88,26 @@ impl<'tcx> LateLintPass<'tcx> for BareDosDeviceNames {
8688
expr.span,
8789
"this path refers to a DOS device",
8890
|diag| {
91+
// Keep `r###` and `###`
92+
let (prefix, hashes) = if let StrStyle::Raw(num) = str_style {
93+
(Cow::Borrowed("r"), "#".repeat(num as usize).into())
94+
} else {
95+
(Cow::Borrowed(""), Cow::Borrowed(""))
96+
};
97+
8998
// Suggest making current behavior explicit
9099
diag.span_suggestion_verbose(
91100
expr.span,
92-
"if this is intended, try",
93-
// FIXME: I have zero clue why it normalizes this. `\` -> `/`
94-
format!(r#"r"\\.\{str_sym}"\"#),
101+
"if this is intended, use",
102+
format!(r#"r{hashes}"\\.\{str_sym}"{hashes}"#),
95103
Applicability::MaybeIncorrect,
96104
);
97105

98106
// Suggest making the code refer to a file or folder in the current directory
99107
diag.span_suggestion_verbose(
100108
expr.span,
101-
"if this was intended to point to a file or folder, try",
102-
format!("\"./{str_sym}\""),
109+
"if this was intended to point to a file or folder, use",
110+
format!(r#"{prefix}{hashes}"./{str_sym}"{hashes}"#),
103111
Applicability::MaybeIncorrect,
104112
);
105113
}

‎tests/ui/bare_dos_device_names.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@aux-build:proc_macros.rs:proc-macro
2-
#![allow(clippy::no_effect, unused)]
2+
#![allow(clippy::needless_raw_string_hashes, clippy::no_effect, unused)]
33
#![warn(clippy::bare_dos_device_names)]
44

55
#[macro_use]
@@ -20,6 +20,9 @@ fn main() {
2020
b("conin$");
2121
File::open("conin$");
2222
std::path::PathBuf::from("a");
23+
// Keep raw string
24+
Path::new(r##"aux"##);
25+
// Don't lint
2326
PathBuf::from("a");
2427
Path::new("a");
2528
external! {

‎tests/ui/bare_dos_device_names.stderr

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ LL | a("con");
55
| ^^^^^
66
|
77
= note: `-D clippy::bare-dos-device-names` implied by `-D warnings`
8-
help: if this is intended, try
8+
help: if this is intended, use
99
|
10-
LL | a(r"//./con"/);
11-
| ~~~~~~~~~~~
12-
help: if this was intended to point to a file or folder, try
10+
LL | a(r"//./con");
11+
| ~~~~~~~~~~
12+
help: if this was intended to point to a file or folder, use
1313
|
1414
LL | a("./con");
1515
| ~~~~~~~
@@ -20,11 +20,11 @@ error: this path refers to a DOS device
2020
LL | b("conin$");
2121
| ^^^^^^^^
2222
|
23-
help: if this is intended, try
23+
help: if this is intended, use
2424
|
25-
LL | b(r"//./conin$"/);
26-
| ~~~~~~~~~~~~~~
27-
help: if this was intended to point to a file or folder, try
25+
LL | b(r"//./conin$");
26+
| ~~~~~~~~~~~~~
27+
help: if this was intended to point to a file or folder, use
2828
|
2929
LL | b("./conin$");
3030
| ~~~~~~~~~~
@@ -35,14 +35,29 @@ error: this path refers to a DOS device
3535
LL | File::open("conin$");
3636
| ^^^^^^^^
3737
|
38-
help: if this is intended, try
38+
help: if this is intended, use
3939
|
40-
LL | File::open(r"//./conin$"/);
41-
| ~~~~~~~~~~~~~~
42-
help: if this was intended to point to a file or folder, try
40+
LL | File::open(r"//./conin$");
41+
| ~~~~~~~~~~~~~
42+
help: if this was intended to point to a file or folder, use
4343
|
4444
LL | File::open("./conin$");
4545
| ~~~~~~~~~~
4646

47-
error: aborting due to 3 previous errors
47+
error: this path refers to a DOS device
48+
--> $DIR/bare_dos_device_names.rs:24:15
49+
|
50+
LL | Path::new(r##"aux"##);
51+
| ^^^^^^^^^^
52+
|
53+
help: if this is intended, use
54+
|
55+
LL | Path::new(r##"//./aux"##);
56+
| ~~~~~~~~~~~~~~
57+
help: if this was intended to point to a file or folder, use
58+
|
59+
LL | Path::new(r##"./aux"##);
60+
| ~~~~~~~~~~~~
61+
62+
error: aborting due to 4 previous errors
4863

0 commit comments

Comments
 (0)
Please sign in to comment.