Skip to content

Commit

Permalink
Use a sorted vector for block comments (#9337)
Browse files Browse the repository at this point in the history
## Summary

I realized that we can avoid allocating a hash map here.
  • Loading branch information
charliermarsh authored Dec 31, 2023
1 parent 686abbc commit cea2ec8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
6 changes: 2 additions & 4 deletions crates/ruff_linter/src/rules/pylint/rules/empty_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_index::Indexer;
use ruff_python_trivia::is_python_whitespace;

use ruff_source_file::Locator;
use ruff_text_size::{TextRange, TextSize};
use rustc_hash::FxHashSet;

/// ## What it does
/// Checks for a # symbol appearing on a line not followed by an actual comment.
Expand Down Expand Up @@ -50,11 +48,11 @@ pub(crate) fn empty_comments(
indexer: &Indexer,
locator: &Locator,
) {
let block_comments = FxHashSet::from_iter(indexer.comment_ranges().block_comments(locator));
let block_comments = indexer.comment_ranges().block_comments(locator);

for range in indexer.comment_ranges() {
// Ignore comments that are part of multi-line "comment blocks".
if block_comments.contains(&range.start()) {
if block_comments.binary_search(&range.start()).is_ok() {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_trivia/src/comment_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl CommentRanges {
/// own-line comments in which the comment hash (`#`) appears in the same
/// column in each line, and at least one comment is non-empty.
///
/// Returns a vector containing the offset of the leading hash (`#`) for
/// each comment in any block comment.
/// Returns a sorted vector containing the offset of the leading hash (`#`)
/// for each comment in any block comment.
///
/// ## Examples
/// ```python
Expand Down

0 comments on commit cea2ec8

Please sign in to comment.