Skip to content

Commit

Permalink
expand inline lint to deref_mut/as_ref/borrow
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Dec 5, 2023
1 parent b2f8437 commit 0f56b84
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/re_types_core/src/loggable_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl From<Box<dyn ComponentBatch>> for MaybeOwnedComponentBatch<'_> {
}

impl<'a> AsRef<dyn ComponentBatch + 'a> for MaybeOwnedComponentBatch<'a> {
#[inline]
fn as_ref(&self) -> &(dyn ComponentBatch + 'a) {
match self {
MaybeOwnedComponentBatch::Owned(this) => &**this,
Expand Down
36 changes: 34 additions & 2 deletions scripts/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,14 @@ def lint_line(
return "Builder methods impls should be marked #[inline]"

# Deref impls should be marked #[inline] or #[inline(always)].
if "fn deref(&self)" in line:
if "fn deref(&self)" in line or "fn deref_mut(&mut self)" in line:
if prev_line_stripped != "#[inline]" and prev_line_stripped != "#[inline(always)]":
return "Deref impls should be marked #[inline]"
return "Deref/DerefMut impls should be marked #[inline]"

# Deref impls should be marked #[inline] or #[inline(always)].
if "fn as_ref(&self)" in line or "fn borrow(&self)" in line:
if prev_line_stripped != "#[inline]" and prev_line_stripped != "#[inline(always)]":
return "as_ref/borrow implementations should be marked #[inline]"

return None

Expand Down Expand Up @@ -220,6 +225,30 @@ def test_lint_line() -> None:
"""
#[inline(always)]
fn deref(&self) -> Self::Target {
""",
"""
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
""",
"""
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
""",
"""
#[inline]
fn borrow(&self) -> &Self {
""",
"""
#[inline(always)]
fn borrow(&self) -> &Self {
""",
"""
#[inline]
fn as_ref(&self) -> &Self {
""",
"""
#[inline(always)]
fn as_ref(&self) -> &Self {
""",
]

Expand Down Expand Up @@ -262,6 +291,9 @@ def test_lint_line() -> None:
"I accidentally wrote the same same word twice",
"fn foo(mut self) -> Self {",
"fn deref(&self) -> Self::Target {",
"fn deref_mut(&mut self) -> &mut Self::Target",
"fn borrow(&self) -> &Self",
"fn as_ref(&self) -> &Self",
]

for test in should_pass:
Expand Down

0 comments on commit 0f56b84

Please sign in to comment.