mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:48:32 +00:00
try String instead of &'static str
This commit is contained in:
parent
7ff37f42b0
commit
9cb02433d5
15 changed files with 50 additions and 56 deletions
|
@ -167,7 +167,7 @@ impl AddAssign for FixMap {
|
|||
let fixed_in_file = self.0.entry(filename).or_default();
|
||||
for (rule, name, count) in fixed.iter() {
|
||||
if count > 0 {
|
||||
*fixed_in_file.entry(rule).or_default(name) += count;
|
||||
*fixed_in_file.entry(rule.clone()).or_default(name) += count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,8 +36,8 @@ bitflags! {
|
|||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ExpandedStatistics {
|
||||
code: Option<NoqaCode>,
|
||||
struct ExpandedStatistics<'a> {
|
||||
code: Option<&'a NoqaCode>,
|
||||
name: &'static str,
|
||||
count: usize,
|
||||
fixable: bool,
|
||||
|
@ -307,7 +307,7 @@ impl Printer {
|
|||
.sorted_by_key(|(code, message)| (*code, message.fixable()))
|
||||
.fold(
|
||||
vec![],
|
||||
|mut acc: Vec<((Option<NoqaCode>, &Message), usize)>, (code, message)| {
|
||||
|mut acc: Vec<((Option<&NoqaCode>, &Message), usize)>, (code, message)| {
|
||||
if let Some(((prev_code, _prev_message), count)) = acc.last_mut() {
|
||||
if *prev_code == code {
|
||||
*count += 1;
|
||||
|
@ -349,12 +349,7 @@ impl Printer {
|
|||
);
|
||||
let code_width = statistics
|
||||
.iter()
|
||||
.map(|statistic| {
|
||||
statistic
|
||||
.code
|
||||
.map_or_else(String::new, |rule| rule.to_string())
|
||||
.len()
|
||||
})
|
||||
.map(|statistic| statistic.code.map_or(0, |code| code.as_str().len()))
|
||||
.max()
|
||||
.unwrap();
|
||||
let any_fixable = statistics.iter().any(|statistic| statistic.fixable);
|
||||
|
@ -370,7 +365,7 @@ impl Printer {
|
|||
statistic.count.to_string().bold(),
|
||||
statistic
|
||||
.code
|
||||
.map_or_else(String::new, |rule| rule.to_string())
|
||||
.map_or_else(String::new, ToString::to_string)
|
||||
.red()
|
||||
.bold(),
|
||||
if any_fixable {
|
||||
|
|
|
@ -82,13 +82,13 @@ pub(crate) fn check_noqa(
|
|||
{
|
||||
let suppressed = match &directive_line.directive {
|
||||
Directive::All(_) => {
|
||||
directive_line.matches.push(code);
|
||||
directive_line.matches.push(code.clone());
|
||||
ignored_diagnostics.push(index);
|
||||
true
|
||||
}
|
||||
Directive::Codes(directive) => {
|
||||
if directive.includes(code) {
|
||||
directive_line.matches.push(code);
|
||||
if directive.includes(&code) {
|
||||
directive_line.matches.push(code.clone());
|
||||
ignored_diagnostics.push(index);
|
||||
true
|
||||
} else {
|
||||
|
|
|
@ -10,8 +10,8 @@ use crate::registry::Linter;
|
|||
use crate::rule_selector::is_single_rule_selector;
|
||||
use crate::rules;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct NoqaCode(&'static str, usize);
|
||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct NoqaCode(String, usize);
|
||||
|
||||
impl NoqaCode {
|
||||
/// Return the prefix for the [`NoqaCode`], e.g., `SIM` for `SIM101`.
|
||||
|
@ -25,7 +25,7 @@ impl NoqaCode {
|
|||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
self.0
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ impl std::fmt::Debug for NoqaCode {
|
|||
|
||||
impl std::fmt::Display for NoqaCode {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
f.write_str(self.0)
|
||||
f.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,7 +55,7 @@ impl serde::Serialize for NoqaCode {
|
|||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.0)
|
||||
serializer.serialize_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -110,7 +110,7 @@ fn apply_fixes<'a>(
|
|||
}
|
||||
|
||||
applied.extend(applied_edits.drain(..));
|
||||
*fixed.entry(code).or_default(name) += 1;
|
||||
*fixed.entry(code.clone()).or_default(name) += 1;
|
||||
}
|
||||
|
||||
// Add the remaining content.
|
||||
|
|
|
@ -105,14 +105,14 @@ impl FixTable {
|
|||
FixTableEntry(self.0.entry(code))
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = (NoqaCode, &'static str, usize)> {
|
||||
pub fn iter(&self) -> impl Iterator<Item = (&NoqaCode, &'static str, usize)> {
|
||||
self.0
|
||||
.iter()
|
||||
.map(|(code, FixCount { rule_name, count })| (*code, *rule_name, *count))
|
||||
.map(|(code, FixCount { rule_name, count })| (code, *rule_name, *count))
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> impl Iterator<Item = NoqaCode> {
|
||||
self.0.keys().copied()
|
||||
pub fn keys(&self) -> impl Iterator<Item = &NoqaCode> {
|
||||
self.0.keys()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
|
@ -710,7 +710,7 @@ pub fn lint_fix<'a>(
|
|||
if iterations < MAX_ITERATIONS {
|
||||
// Count the number of fixed errors.
|
||||
for (rule, name, count) in applied.iter() {
|
||||
*fixed.entry(rule).or_default(name) += count;
|
||||
*fixed.entry(rule.clone()).or_default(name) += count;
|
||||
}
|
||||
|
||||
transformed = Cow::Owned(transformed.updated(fixed_contents, &source_map));
|
||||
|
@ -737,10 +737,10 @@ pub fn lint_fix<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
fn collect_rule_codes(rules: impl IntoIterator<Item = NoqaCode>) -> String {
|
||||
fn collect_rule_codes<'a>(rules: impl IntoIterator<Item = &'a NoqaCode>) -> String {
|
||||
rules
|
||||
.into_iter()
|
||||
.map(|rule| rule.to_string())
|
||||
.map(ToString::to_string)
|
||||
.sorted_unstable()
|
||||
.dedup()
|
||||
.join(", ")
|
||||
|
@ -780,11 +780,11 @@ This indicates a bug in Ruff. If you could open an issue at:
|
|||
}
|
||||
|
||||
#[expect(clippy::print_stderr)]
|
||||
fn report_fix_syntax_error(
|
||||
fn report_fix_syntax_error<'a>(
|
||||
path: &Path,
|
||||
transformed: &str,
|
||||
error: &ParseError,
|
||||
rules: impl IntoIterator<Item = NoqaCode>,
|
||||
rules: impl IntoIterator<Item = &'a NoqaCode>,
|
||||
) {
|
||||
let codes = collect_rule_codes(rules);
|
||||
if cfg!(debug_assertions) {
|
||||
|
|
|
@ -81,7 +81,7 @@ pub(crate) fn message_to_json_value(message: &Message, context: &EmitterContext)
|
|||
}
|
||||
|
||||
json!({
|
||||
"code": message.noqa_code().map(|code| code.to_string()),
|
||||
"code": message.noqa_code().map(ToString::to_string),
|
||||
"url": message.to_url(),
|
||||
"message": message.body(),
|
||||
"fix": fix,
|
||||
|
|
|
@ -225,8 +225,8 @@ impl Message {
|
|||
}
|
||||
|
||||
/// Returns the [`NoqaCode`] corresponding to the diagnostic message.
|
||||
pub fn noqa_code(&self) -> Option<NoqaCode> {
|
||||
self.noqa_code
|
||||
pub fn noqa_code(&self) -> Option<&NoqaCode> {
|
||||
self.noqa_code.as_ref()
|
||||
}
|
||||
|
||||
/// Returns the URL for the rule documentation, if it exists.
|
||||
|
|
|
@ -71,7 +71,7 @@ fn message_to_rdjson_value(message: &Message) -> Value {
|
|||
"range": rdjson_range(start_location, end_location),
|
||||
},
|
||||
"code": {
|
||||
"value": message.noqa_code().map(|code| code.to_string()),
|
||||
"value": message.noqa_code().map(ToString::to_string),
|
||||
"url": message.to_url(),
|
||||
},
|
||||
"suggestions": rdjson_suggestions(fix.edits(), &source_code),
|
||||
|
@ -84,7 +84,7 @@ fn message_to_rdjson_value(message: &Message) -> Value {
|
|||
"range": rdjson_range(start_location, end_location),
|
||||
},
|
||||
"code": {
|
||||
"value": message.noqa_code().map(|code| code.to_string()),
|
||||
"value": message.noqa_code().map(ToString::to_string),
|
||||
"url": message.to_url(),
|
||||
},
|
||||
})
|
||||
|
|
|
@ -61,8 +61,8 @@ struct SarifRule<'a> {
|
|||
url: Option<String>,
|
||||
}
|
||||
|
||||
impl From<NoqaCode> for SarifRule<'_> {
|
||||
fn from(code: NoqaCode) -> Self {
|
||||
impl From<&NoqaCode> for SarifRule<'_> {
|
||||
fn from(code: &NoqaCode) -> Self {
|
||||
let code_str = code.to_string();
|
||||
// This is a manual re-implementation of Rule::from_code, but we also want the Linter. This
|
||||
// avoids calling Linter::parse_code twice.
|
||||
|
@ -111,8 +111,8 @@ impl Serialize for SarifRule<'_> {
|
|||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct SarifResult {
|
||||
code: Option<NoqaCode>,
|
||||
struct SarifResult<'a> {
|
||||
code: Option<&'a NoqaCode>,
|
||||
level: String,
|
||||
message: String,
|
||||
uri: String,
|
||||
|
@ -122,9 +122,9 @@ struct SarifResult {
|
|||
end_column: OneIndexed,
|
||||
}
|
||||
|
||||
impl SarifResult {
|
||||
impl<'a> SarifResult<'a> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn from_message(message: &Message) -> Result<Self> {
|
||||
fn from_message(message: &'a Message) -> Result<Self> {
|
||||
let start_location = message.compute_start_location();
|
||||
let end_location = message.compute_end_location();
|
||||
let path = normalize_path(&*message.filename());
|
||||
|
@ -144,7 +144,7 @@ impl SarifResult {
|
|||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[expect(clippy::unnecessary_wraps)]
|
||||
fn from_message(message: &Message) -> Result<Self> {
|
||||
fn from_message(message: &'a Message) -> Result<Self> {
|
||||
let start_location = message.compute_start_location();
|
||||
let end_location = message.compute_end_location();
|
||||
let path = normalize_path(&*message.filename());
|
||||
|
@ -161,7 +161,7 @@ impl SarifResult {
|
|||
}
|
||||
}
|
||||
|
||||
impl Serialize for SarifResult {
|
||||
impl Serialize for SarifResult<'_> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
|
@ -184,7 +184,7 @@ impl Serialize for SarifResult {
|
|||
}
|
||||
}
|
||||
}],
|
||||
"ruleId": self.code.map(|code| code.to_string()),
|
||||
"ruleId": self.code.map(ToString::to_string),
|
||||
})
|
||||
.serialize(serializer)
|
||||
}
|
||||
|
|
|
@ -255,7 +255,7 @@ impl Display for MessageCodeFrame<'_> {
|
|||
let label = self
|
||||
.message
|
||||
.noqa_code()
|
||||
.map_or_else(String::new, |code| code.to_string());
|
||||
.map_or_else(String::new, ToString::to_string);
|
||||
|
||||
let line_start = self.notebook_index.map_or_else(
|
||||
|| start_index.get(),
|
||||
|
|
|
@ -106,9 +106,9 @@ impl Codes<'_> {
|
|||
|
||||
/// Returns `true` if the string list of `codes` includes `code` (or an alias
|
||||
/// thereof).
|
||||
pub(crate) fn includes(&self, needle: NoqaCode) -> bool {
|
||||
pub(crate) fn includes(&self, needle: &NoqaCode) -> bool {
|
||||
self.iter()
|
||||
.any(|code| needle == get_redirect_target(code.as_str()).unwrap_or(code.as_str()))
|
||||
.any(|code| needle == &get_redirect_target(code.as_str()).unwrap_or(code.as_str()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -140,7 +140,7 @@ pub(crate) fn rule_is_ignored(
|
|||
Ok(Some(NoqaLexerOutput {
|
||||
directive: Directive::Codes(codes),
|
||||
..
|
||||
})) => codes.includes(code.noqa_code()),
|
||||
})) => codes.includes(&code.noqa_code()),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
@ -830,7 +830,7 @@ fn build_noqa_edits_by_line<'a>(
|
|||
|
||||
struct NoqaComment<'a> {
|
||||
line: TextSize,
|
||||
code: NoqaCode,
|
||||
code: &'a NoqaCode,
|
||||
directive: Option<&'a Directive<'a>>,
|
||||
}
|
||||
|
||||
|
@ -859,7 +859,7 @@ fn find_noqa_comments<'a>(
|
|||
}
|
||||
FileExemption::Codes(codes) => {
|
||||
// If the diagnostic is ignored by a global exemption, don't add a noqa directive.
|
||||
if codes.contains(&&code) {
|
||||
if codes.contains(&code) {
|
||||
comments_by_line.push(None);
|
||||
continue;
|
||||
}
|
||||
|
@ -921,7 +921,7 @@ fn find_noqa_comments<'a>(
|
|||
|
||||
struct NoqaEdit<'a> {
|
||||
edit_range: TextRange,
|
||||
noqa_codes: FxHashSet<NoqaCode>,
|
||||
noqa_codes: FxHashSet<&'a NoqaCode>,
|
||||
codes: Option<&'a Codes<'a>>,
|
||||
line_ending: LineEnding,
|
||||
}
|
||||
|
@ -964,7 +964,7 @@ impl Ranged for NoqaEdit<'_> {
|
|||
fn generate_noqa_edit<'a>(
|
||||
directive: Option<&'a Directive>,
|
||||
offset: TextSize,
|
||||
noqa_codes: FxHashSet<NoqaCode>,
|
||||
noqa_codes: FxHashSet<&'a NoqaCode>,
|
||||
locator: &Locator,
|
||||
line_ending: LineEnding,
|
||||
) -> Option<NoqaEdit<'a>> {
|
||||
|
|
|
@ -234,7 +234,7 @@ Source with applied fixes:
|
|||
|
||||
let messages = messages
|
||||
.into_iter()
|
||||
.filter_map(|msg| Some((msg.noqa_code()?, msg)))
|
||||
.filter_map(|msg| Some((msg.noqa_code().cloned()?, msg)))
|
||||
.map(|(code, mut diagnostic)| {
|
||||
let rule = Rule::from_code(code.as_str()).unwrap();
|
||||
let fixable = diagnostic.fix().is_some_and(|fix| {
|
||||
|
|
|
@ -290,9 +290,8 @@ See also https://github.com/astral-sh/ruff/issues/2186.
|
|||
|
||||
rule_noqa_code_match_arms.extend(quote! {
|
||||
#(#attrs)* Rule::#rule_name => {
|
||||
static CODE: std::sync::LazyLock<String> = std::sync::LazyLock::new(
|
||||
|| format!("{}{}", crate::registry::Linter::#linter.common_prefix(), #code));
|
||||
NoqaCode(CODE.as_str(), crate::registry::Linter::#linter.common_prefix().len())
|
||||
let prefix = crate::registry::Linter::#linter.common_prefix();
|
||||
NoqaCode(format!("{}{}", prefix, #code), prefix.len())
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -208,7 +208,7 @@ impl Workspace {
|
|||
let messages: Vec<ExpandedMessage> = messages
|
||||
.into_iter()
|
||||
.map(|msg| ExpandedMessage {
|
||||
code: msg.noqa_code().map(|code| code.to_string()),
|
||||
code: msg.noqa_code().map(ToString::to_string),
|
||||
message: msg.body().to_string(),
|
||||
start_location: source_code.line_column(msg.start()).into(),
|
||||
end_location: source_code.line_column(msg.end()).into(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue