Skip to content

Commit 34bb0cb

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 bcfa2c0 commit 34bb0cb

20 files changed

+74
-36
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5022,8 +5022,8 @@ Released 2018-09-13
50225022
[`needless_pass_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
50235023
[`needless_question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark
50245024
[`needless_range_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop
5025-
[`needless_raw_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_raw_string
50265025
[`needless_raw_string_hashes`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_raw_string_hashes
5026+
[`needless_raw_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_raw_strings
50275027
[`needless_return`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
50285028
[`needless_splitn`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_splitn
50295029
[`needless_update`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_update
@@ -5388,5 +5388,5 @@ Released 2018-09-13
53885388
[`min-ident-chars-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#min-ident-chars-threshold
53895389
[`accept-comment-above-statement`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-statement
53905390
[`accept-comment-above-attributes`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-attributes
5391-
[`allow-one-hash-in-raw-string`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-string
5391+
[`allow-one-hash-in-raw-strings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-strings
53925392
<!-- end autogenerated links to configuration documentation -->

book/src/lint_configuration.md

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

718718

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

722722
**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
@@ -1064,6 +1064,12 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10641064
def_id_to_usage: rustc_data_structures::fx::FxHashMap::default(),
10651065
})
10661066
});
1067+
let needless_raw_string_hashes_allow_one = conf.allow_one_hash_in_raw_strings;
1068+
store.register_early_pass(move || {
1069+
Box::new(raw_strings::RawStrings {
1070+
needless_raw_string_hashes_allow_one,
1071+
})
1072+
});
10671073
// add lints here, do not remove this comment, it's used in `new_lint`
10681074
}
10691075

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
@@ -549,7 +549,7 @@ define_Conf! {
549549
/// Lint: UNNECESSARY_RAW_STRING_HASHES.
550550
///
551551
/// Whether to allow `r#""#` when `r""` can be used
552-
(allow_one_hash_in_raw_string: bool = false),
552+
(allow_one_hash_in_raw_strings: bool = false),
553553
}
554554

555555
/// Search for the configuration file.

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)