Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions crates/ruff_db/src/diagnostic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ impl Diagnostic {
/// should be constructed via [`Annotation::primary`]. A diagnostic with no
/// primary annotations is allowed, but its rendering may be sub-optimal.
pub fn annotate(&mut self, ann: Annotation) {
Arc::make_mut(&mut self.inner).annotations.push(ann);
let annotations = &mut Arc::make_mut(&mut self.inner).annotations;
// Most diagnostics have exactly one annotation. Avoid the default allocation of four
// annotations on the first push, while retaining normal amortized growth thereafter.
if annotations.is_empty() {
annotations.reserve_exact(1);
}
annotations.push(ann);
}

/// Adds an "info" sub-diagnostic with the given message.
Expand All @@ -172,9 +178,11 @@ impl Diagnostic {

/// Adds an "info" sub-diagnostic before any existing sub-diagnostics.
pub fn prepend_info<'a>(&mut self, message: impl IntoDiagnosticMessage + 'a) {
Arc::make_mut(&mut self.inner)
.subs
.insert(0, SubDiagnostic::new(SubDiagnosticSeverity::Info, message));
let subs = &mut Arc::make_mut(&mut self.inner).subs;
if subs.is_empty() {
subs.reserve_exact(1);
}
subs.insert(0, SubDiagnostic::new(SubDiagnosticSeverity::Info, message));
}

/// Adds a "help" sub-diagnostic with the given message.
Expand All @@ -190,7 +198,11 @@ impl Diagnostic {
/// to it. For the simpler case of a sub-diagnostic with only a message,
/// using a method like [`Diagnostic::info`] may be more convenient.
pub fn sub(&mut self, sub: SubDiagnostic) {
Arc::make_mut(&mut self.inner).subs.push(sub);
let subs = &mut Arc::make_mut(&mut self.inner).subs;
if subs.is_empty() {
subs.reserve_exact(1);
}
subs.push(sub);
}

/// Return a `std::fmt::Display` implementation that renders this
Expand Down Expand Up @@ -685,6 +697,9 @@ impl SubDiagnostic {
/// to have no annotations (e.g., a simple note) than for a diagnostic to
/// have no annotations.
pub fn annotate(&mut self, ann: Annotation) {
if self.inner.annotations.is_empty() {
self.inner.annotations.reserve_exact(1);
}
self.inner.annotations.push(ann);
}

Expand Down
Loading