Skip to content

Conversation

nils-degroot
Copy link

@nils-degroot nils-degroot commented May 15, 2025

This pr adds the manual_as_slice lint.

closes: #7633


changelog: add [manual_as_slice] lint

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 15, 2025
Copy link
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a good starting point. I'd want some more test cases, and there is some duplication we can remove, but otherwise this seems mostly merge-worthy. I'd like to rename the lint though, it's too wordy. How about manual_as_slice? That fits very well into our naming structure. Also please squash your commits when ready.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 16, 2025
@nils-degroot
Copy link
Author

I like manual_as_slice, it shows the intended behavior for the lint. I'll make the changes

@nils-degroot nils-degroot force-pushed the master branch 2 times, most recently from 1a8a066 to dc4e07b Compare May 16, 2025 13:51
@llogiq
Copy link
Contributor

llogiq commented May 16, 2025

I think you need to re-bless the tests.

@llogiq llogiq changed the title changelog: Add as_slice_instead_of_reference_full_range lint Add manual_as_slice lint May 16, 2025
Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should at least check that:

  • the resulting type is a slice
  • the .as_slice()/.as_mut_slice() methods exist on the original type

or restrict the original type to those coming from core/alloc.

Otherwise, you take the risk of having a type implement [..] for something else (let's say, in a domain-specific-language) without having .as_slice() available. Here is an example:

#![feature(new_range_api)]
use std::ops::Index;
use std::range::RangeBounds;

struct Count;
impl<R: RangeBounds<()>> Index<R> for Count {
    type Output = ();
    fn index(&self, _: R) -> &Self::Output {
        &()
    }
}

fn main() {
    _ = &Count[..];
}

It will suggest to use Count.as_slice() even though this method does not exist on Count.

@nils-degroot
Copy link
Author

nils-degroot commented May 18, 2025

I'm gonna restrict is to Vec, array and slice in that case, since doing this on a string also would not work

@nils-degroot
Copy link
Author

Is it possible to get this working on vec since its not a language item?

@samueltardieu
Copy link
Member

Is it possible to get this working on vec since its not a language item?

Language items are items that the compiler needs when it transforms code. For example, it needs panic!() as a language item, as it will generate calls to it. alloc::vec::Vec is not in this category, as the compiler never needs to transform the code in a way that would build a vector.

However, Vec is a diagnostic item, which means that you can get methods from clippy_utils (look for "diagnostic" there) to identify whether something is a Vec or not. Most of the things Clippy needs to identify in the standard library are marked as diagnostic items, and we can mark new items if we need them, so that we can easily use them in lints.

@nils-degroot nils-degroot requested a review from llogiq May 27, 2025 05:36
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 27, 2025
Copy link
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like the message to be shorter. Otherwise this looks mostly good. We may want to select a different lint group though. I'll start the final comment period soon.

@nils-degroot nils-degroot force-pushed the master branch 3 times, most recently from 545c550 to a8a448d Compare May 31, 2025 06:45
@rustbot

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Jun 5, 2025

☔ The latest upstream changes (possibly b379d54) made this pull request unmergeable. Please resolve the merge conflicts.

Comment on lines +31 to +38
macro_rules! perform_the_slice {
($a:expr) => {
&$a[..]
//~^ manual_as_slice
};
}

perform_the_slice!([1, 2, 3]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we shouldn't lint inside macro definitions, because they may be called with arguments of different types, e.g. the following would be valid:

macro_rules! perform_the_slice {
    ($a:expr) => {
        &$a[..]
        //~^ manual_as_slice
    };
}

perform_the_slice!([1, 2, 3]);
perform_the_slice!(String::new());

and putting the .as_slice() would break the second call

Comment on lines +42 to +48
let sugg_tail = match mutability {
Mutability::Not => ".as_slice()",
Mutability::Mut => ".as_mut_slice()",
};

let borrow_span = expr.span.until(borrow.span);
let app = Applicability::MachineApplicable;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could construct all of these inside the |diag| { .. } closure below. That's probably marginally faster, but, more importantly, clearer, as otherwise one could think they're going to be used in some lint logic

@ada4a
Copy link
Contributor

ada4a commented Oct 11, 2025

since doing this on a string also would not work

I think it would work, you'd just need to suggest as_str/as_str_mut instead. But this could be left as a future improvement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest usage of array.as_slice() (instead of &array[..]) and array.as_mut_slice()

5 participants