Skip to content

Add manual_as_slice lint - #14809

Open
nils-degroot wants to merge 1 commit into
rust-lang:masterfrom
nils-degroot:master
Open

nils-degroot wants to merge 1 commit into
rust-lang:masterfrom
nils-degroot:master

Conversation

@nils-degroot

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

Copy link
Copy Markdown

View all comments

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

@llogiq llogiq left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread clippy_lints/src/as_slice_instead_of_reference_full_range.rs Outdated
Comment thread tests/ui/as_slice_instead_of_reference_full_range.rs Outdated
@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
Copy Markdown
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

llogiq commented May 16, 2025

Copy link
Copy Markdown
Contributor

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
Comment thread tests/ui/manual_as_slice.fixed Outdated

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

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

nils-degroot commented May 18, 2025

Copy link
Copy Markdown
Author

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
Copy Markdown
Author

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

@samueltardieu

Copy link
Copy Markdown
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

@llogiq llogiq left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread clippy_lints/src/manual_as_slice.rs Outdated
@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

This comment has been minimized.

@nils-degroot
nils-degroot force-pushed the master branch 2 times, most recently from 6f02288 to 750f2da Compare June 11, 2025 18:00
Comment thread tests/ui/manual_as_slice.rs
Comment thread clippy_lints/src/manual_as_slice.rs Outdated
@ada4a

ada4a commented Oct 11, 2025

Copy link
Copy Markdown
Contributor

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

@Jarcho

Jarcho commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Ping @nils-degroot are you willing to continue working on this?

r? Jarcho

@rustbot rustbot assigned Jarcho and unassigned llogiq Sep 2, 2026
@nils-degroot

Copy link
Copy Markdown
Author

Hey, forgot this was open. I'll handle the comments and rebase now

@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Sep 16, 2026
@rustbot

rustbot commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@nils-degroot
nils-degroot requested a review from ada4a September 16, 2026 19:21
@github-actions

Copy link
Copy Markdown

Lintcheck changes for dabee41

Lint Added Removed Changed
clippy::manual_as_slice 201 0 0

This comment will be updated if you push new changes

@ada4a ada4a left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

///
/// ### Why is this bad?
///
/// Using the `some_value.as_slice()` method is more explicit then using `&some_value[..]`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Using the `some_value.as_slice()` method is more explicit then using `&some_value[..]`
/// Using the `some_value.as_slice()` method is more explicit than using `&some_value[..]`

/// let array: [u8; 4] = [0; 4];
/// let slice = array.as_slice();
/// ```
#[clippy::version = "1.88.0"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[clippy::version = "1.88.0"]
#[clippy::version = "1.100.0"]

/// ```
#[clippy::version = "1.88.0"]
pub MANUAL_AS_SLICE,
nursery,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
nursery,
pedantic,

Comment on lines +39 to +43
if expr.span.from_expansion() {
return;
}

if let ExprKind::AddrOf(_, mutability, borrow) = expr.kind

@ada4a ada4a Sep 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: could extend the let-chain to make the code more concise

Suggested change
if expr.span.from_expansion() {
return;
}
if let ExprKind::AddrOf(_, mutability, borrow) = expr.kind
if !expr.span.from_expansion()
&& let ExprKind::AddrOf(_, mutability, borrow) = expr.kind

};

diag.multipart_suggestion(
"try",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let's make this a bit more descriptive

Suggested change
"try",
format!("use `{sugg_tail}` instead"),

Comment on lines +49 to +55
{
match cx.typeck_results().expr_ty(value).kind() {
ty::Array(_, _) | ty::Slice(_) => {},
ty::Ref(_, t, _) if let ty::Array(_, _) | ty::Slice(_) = t.kind() => {},
ty::Adt(adt, _) if cx.tcx.is_diagnostic_item(sym::Vec, adt.did()) => {},
_ => return,
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you could simplify this down to

Suggested change
{
match cx.typeck_results().expr_ty(value).kind() {
ty::Array(_, _) | ty::Slice(_) => {},
ty::Ref(_, t, _) if let ty::Array(_, _) | ty::Slice(_) = t.kind() => {},
ty::Adt(adt, _) if cx.tcx.is_diagnostic_item(sym::Vec, adt.did()) => {},
_ => return,
}
&& !is_slice_like(cx.typeck_results().expr_ty_adjusted(value))
{

using clippy_utils::is_slice_like

ty::Adt(adt, _) if cx.tcx.is_diagnostic_item(sym::Vec, adt.did()) => {},
_ => return,
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lintcheck shows that this lint currently fires on &[][..], for which [].as_slice() sounds overkill imo... consider not linting in that case?

@rustbot rustbot added lint-nominated Create an FCP-thread on Zulip for this PR and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Sep 17, 2026
@rustbot

rustbot commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Sep 17, 2026
@rustbot

rustbot commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

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

Labels

lint-nominated Create an FCP-thread on Zulip for this PR needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

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()

6 participants