Skip to content

Commit 8cb6c86

Browse files
committed
change category and refactor
Update raw_strings.rs Revert "new lints for visibility" This reverts commit 0e5a537. new lints for visibility
1 parent cb52d19 commit 8cb6c86

21 files changed

+75
-37
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5048,8 +5048,8 @@ Released 2018-09-13
50485048
[`needless_pass_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
50495049
[`needless_question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark
50505050
[`needless_range_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop
5051-
[`needless_raw_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_raw_string
50525051
[`needless_raw_string_hashes`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_raw_string_hashes
5052+
[`needless_raw_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_raw_strings
50535053
[`needless_return`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
50545054
[`needless_splitn`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_splitn
50555055
[`needless_update`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_update
@@ -5414,5 +5414,5 @@ Released 2018-09-13
54145414
[`min-ident-chars-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#min-ident-chars-threshold
54155415
[`accept-comment-above-statement`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-statement
54165416
[`accept-comment-above-attributes`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-attributes
5417-
[`allow-one-hash-in-raw-string`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-string
5417+
[`allow-one-hash-in-raw-strings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-strings
54185418
<!-- end autogenerated links to configuration documentation -->

book/src/lint_configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ Whether to accept a safety comment to be placed above the attributes for the `un
717717
* [`undocumented_unsafe_blocks`](https://rust-lang.github.io/rust-clippy/master/index.html#undocumented_unsafe_blocks)
718718

719719

720-
## `allow-one-hash-in-raw-string`
720+
## `allow-one-hash-in-raw-strings`
721721
Whether to allow `r#""#` when `r""` can be used
722722

723723
**Default Value:** `false` (`bool`)

clippy_lints/src/declared_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
540540
crate::ranges::RANGE_MINUS_ONE_INFO,
541541
crate::ranges::RANGE_PLUS_ONE_INFO,
542542
crate::ranges::REVERSED_EMPTY_RANGES_INFO,
543-
crate::raw_strings::NEEDLESS_RAW_STRING_INFO,
543+
crate::raw_strings::NEEDLESS_RAW_STRINGS_INFO,
544544
crate::raw_strings::NEEDLESS_RAW_STRING_HASHES_INFO,
545545
crate::rc_clone_in_vec_init::RC_CLONE_IN_VEC_INIT_INFO,
546546
crate::read_zero_byte_vec::READ_ZERO_BYTE_VEC_INFO,

clippy_lints/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,12 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10621062
def_id_to_usage: rustc_data_structures::fx::FxHashMap::default(),
10631063
})
10641064
});
1065+
let needless_raw_string_hashes_allow_one = conf.allow_one_hash_in_raw_strings;
1066+
store.register_early_pass(move || {
1067+
Box::new(raw_strings::RawStrings {
1068+
needless_raw_string_hashes_allow_one,
1069+
})
1070+
});
10651071
// add lints here, do not remove this comment, it's used in `new_lint`
10661072
}
10671073

clippy_lints/src/raw_strings.rs

+48-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::{iter::once, ops::ControlFlow};
2+
13
use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet};
24
use rustc_ast::{
35
ast::{Expr, ExprKind},
@@ -13,7 +15,8 @@ declare_clippy_lint! {
1315
/// Checks for raw string literals where a string literal can be used instead.
1416
///
1517
/// ### Why is this bad?
16-
/// It's just unnecessary.
18+
/// It's just unnecessary, but there are many cases where using a raw string literal is more
19+
/// idiomatic than a string literal, so it's opt-in.
1720
///
1821
/// ### Example
1922
/// ```rust
@@ -24,8 +27,8 @@ declare_clippy_lint! {
2427
/// let r = "Hello, world!";
2528
/// ```
2629
#[clippy::version = "1.72.0"]
27-
pub NEEDLESS_RAW_STRING,
28-
complexity,
30+
pub NEEDLESS_RAW_STRINGS,
31+
restriction,
2932
"suggests using a string literal when a raw string literal is unnecessary"
3033
}
3134
declare_clippy_lint! {
@@ -46,10 +49,10 @@ declare_clippy_lint! {
4649
/// ```
4750
#[clippy::version = "1.72.0"]
4851
pub NEEDLESS_RAW_STRING_HASHES,
49-
complexity,
52+
style,
5053
"suggests reducing the number of hashes around a raw string literal"
5154
}
52-
impl_lint_pass!(RawStrings => [NEEDLESS_RAW_STRING, NEEDLESS_RAW_STRING_HASHES]);
55+
impl_lint_pass!(RawStrings => [NEEDLESS_RAW_STRINGS, NEEDLESS_RAW_STRING_HASHES]);
5356

5457
pub struct RawStrings {
5558
pub needless_raw_string_hashes_allow_one: bool,
@@ -59,8 +62,9 @@ impl EarlyLintPass for RawStrings {
5962
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
6063
if !in_external_macro(cx.sess(), expr.span)
6164
&& let ExprKind::Lit(lit) = expr.kind
62-
&& let LitKind::StrRaw(num) | LitKind::ByteStrRaw(num) | LitKind::CStrRaw(num) = lit.kind
65+
&& let LitKind::StrRaw(max) | LitKind::ByteStrRaw(max) | LitKind::CStrRaw(max) = lit.kind
6366
{
67+
let str = lit.symbol.as_str();
6468
let prefix = match lit.kind {
6569
LitKind::StrRaw(..) => "r",
6670
LitKind::ByteStrRaw(..) => "br",
@@ -71,10 +75,10 @@ impl EarlyLintPass for RawStrings {
7175
return;
7276
}
7377

74-
if !lit.symbol.as_str().contains(['\\', '"']) {
78+
if !str.contains(['\\', '"']) {
7579
span_lint_and_sugg(
7680
cx,
77-
NEEDLESS_RAW_STRING,
81+
NEEDLESS_RAW_STRINGS,
7882
expr.span,
7983
"unnecessary raw string literal",
8084
"try",
@@ -85,15 +89,43 @@ impl EarlyLintPass for RawStrings {
8589
return;
8690
}
8791

88-
#[expect(clippy::cast_possible_truncation)]
89-
let req = lit.symbol.as_str().as_bytes()
90-
.split(|&b| b == b'"')
91-
.skip(1)
92-
.map(|bs| 1 + bs.iter().take_while(|&&b| b == b'#').count() as u8)
93-
.max()
94-
.unwrap_or(0);
92+
let req = {
93+
let mut following_quote = false;
94+
let mut req = 0;
95+
// `once` so a raw string ending in hashes is still checked
96+
let num = str.as_bytes().iter().chain(once(&0)).try_fold(0u8, |acc, &b| {
97+
match b {
98+
b'"' => (following_quote, req) = (true, 1),
99+
// I'm a bit surprised the compiler didn't optimize this out, there's no
100+
// branch but it still ends up doing an unnecessary comparison, it's:
101+
// - cmp r9b,1h
102+
// - sbb cl,-1h
103+
// which will add 1 if it's true. With this change, it becomes:
104+
// - add cl,r9b
105+
// isn't that so much nicer?
106+
b'#' => req += u8::from(following_quote),
107+
_ => {
108+
if following_quote {
109+
following_quote = false;
110+
111+
if req == max {
112+
return ControlFlow::Break(req);
113+
}
114+
115+
return ControlFlow::Continue(acc.max(req));
116+
}
117+
},
118+
}
119+
120+
ControlFlow::Continue(acc)
121+
});
122+
123+
match num {
124+
ControlFlow::Continue(num) | ControlFlow::Break(num) => num,
125+
}
126+
};
95127

96-
if req < num {
128+
if req < max {
97129
let hashes = "#".repeat(req as usize);
98130

99131
span_lint_and_sugg(

clippy_lints/src/utils/conf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ define_Conf! {
550550
/// Lint: UNNECESSARY_RAW_STRING_HASHES.
551551
///
552552
/// Whether to allow `r#""#` when `r""` can be used
553-
(allow_one_hash_in_raw_string: bool = false),
553+
(allow_one_hash_in_raw_strings: bool = false),
554554
}
555555

556556
/// Search for the configuration file.

tests/compile-test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn base_config(test_dir: &str) -> compiletest::Config {
5050
config.program.args.push("-Dwarnings".into());
5151

5252
// Normalize away slashes in windows paths.
53-
config.stderr_filter(r#"\\"#, "/");
53+
config.stderr_filter(r"\\", "/");
5454

5555
//config.build_base = profile_path.join("test").join(test_dir);
5656
config.program.program = profile_path.join(if cfg!(windows) {

tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@compile-flags: --crate-name conf_disallowed_methods
22

3-
#![allow(clippy::needless_raw_string)]
3+
#![allow(clippy::needless_raw_strings)]
44
#![warn(clippy::disallowed_methods)]
55
#![allow(clippy::useless_vec)]
66

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
44
allow-dbg-in-tests
55
allow-expect-in-tests
66
allow-mixed-uninlined-format-args
7-
allow-one-hash-in-raw-string
7+
allow-one-hash-in-raw-strings
88
allow-print-in-tests
99
allow-private-module-inception
1010
allow-unwrap-in-tests
@@ -73,7 +73,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
7373
allow-dbg-in-tests
7474
allow-expect-in-tests
7575
allow-mixed-uninlined-format-args
76-
allow-one-hash-in-raw-string
76+
allow-one-hash-in-raw-strings
7777
allow-print-in-tests
7878
allow-private-module-inception
7979
allow-unwrap-in-tests

tests/ui/needless_raw_string.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@run-rustfix
22
#![allow(clippy::needless_raw_string_hashes, clippy::no_effect, unused)]
3-
#![warn(clippy::needless_raw_string)]
3+
#![warn(clippy::needless_raw_strings)]
44
#![feature(c_str_literals)]
55

66
fn main() {

tests/ui/needless_raw_string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@run-rustfix
22
#![allow(clippy::needless_raw_string_hashes, clippy::no_effect, unused)]
3-
#![warn(clippy::needless_raw_string)]
3+
#![warn(clippy::needless_raw_strings)]
44
#![feature(c_str_literals)]
55

66
fn main() {

tests/ui/needless_raw_string.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: unnecessary raw string literal
44
LL | r#"aaa"#;
55
| ^^^^^^^^ help: try: `"aaa"`
66
|
7-
= note: `-D clippy::needless-raw-string` implied by `-D warnings`
7+
= note: `-D clippy::needless-raw-strings` implied by `-D warnings`
88

99
error: unnecessary raw string literal
1010
--> $DIR/needless_raw_string.rs:10:5

tests/ui/needless_raw_string_hashes.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@run-rustfix
2-
#![allow(clippy::needless_raw_string, clippy::no_effect, unused)]
2+
#![allow(clippy::no_effect, unused)]
33
#![warn(clippy::needless_raw_string_hashes)]
44
#![feature(c_str_literals)]
55

tests/ui/needless_raw_string_hashes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@run-rustfix
2-
#![allow(clippy::needless_raw_string, clippy::no_effect, unused)]
2+
#![allow(clippy::no_effect, unused)]
33
#![warn(clippy::needless_raw_string_hashes)]
44
#![feature(c_str_literals)]
55

tests/ui/regex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(
22
unused,
3-
clippy::needless_raw_string,
3+
clippy::needless_raw_strings,
44
clippy::needless_raw_string_hashes,
55
clippy::needless_borrow
66
)]

tests/ui/single_char_add_str.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@run-rustfix
22
#![warn(clippy::single_char_add_str)]
3-
#![allow(clippy::needless_raw_string, clippy::needless_raw_string_hashes)]
3+
#![allow(clippy::needless_raw_strings, clippy::needless_raw_string_hashes)]
44

55
macro_rules! get_string {
66
() => {

tests/ui/single_char_add_str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@run-rustfix
22
#![warn(clippy::single_char_add_str)]
3-
#![allow(clippy::needless_raw_string, clippy::needless_raw_string_hashes)]
3+
#![allow(clippy::needless_raw_strings, clippy::needless_raw_string_hashes)]
44

55
macro_rules! get_string {
66
() => {

tests/ui/single_char_pattern.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@run-rustfix
22

3-
#![allow(clippy::needless_raw_string, clippy::needless_raw_string_hashes, unused_must_use)]
3+
#![allow(clippy::needless_raw_strings, clippy::needless_raw_string_hashes, unused_must_use)]
44

55
use std::collections::HashSet;
66

tests/ui/single_char_pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@run-rustfix
22

3-
#![allow(clippy::needless_raw_string, clippy::needless_raw_string_hashes, unused_must_use)]
3+
#![allow(clippy::needless_raw_strings, clippy::needless_raw_string_hashes, unused_must_use)]
44

55
use std::collections::HashSet;
66

tests/ui/write_literal_2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(unused_must_use)]
2-
#![warn(clippy::needless_raw_string, clippy::write_literal)]
2+
#![warn(clippy::needless_raw_strings, clippy::write_literal)]
33

44
use std::io::Write;
55

tests/ui/write_literal_2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: unnecessary raw string literal
44
LL | writeln!(v, r"{}", r"{hello}");
55
| ^^^^^^^^^^ help: try: `"{hello}"`
66
|
7-
= note: `-D clippy::needless-raw-string` implied by `-D warnings`
7+
= note: `-D clippy::needless-raw-strings` implied by `-D warnings`
88

99
error: literal with an empty format string
1010
--> $DIR/write_literal_2.rs:9:23

0 commit comments

Comments
 (0)