rust-lang / rust-lang/rfcs

Rustc should throw a helpful warning on useless "self" argument in methods.

Open
#1,827 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

T-compiler
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

When we have a code which does not need the self object we may simply say that it can be removed.

struct A(u64);
impl A {
    fn m1(&self) { // warning: unused self can be removed
        println!("Hello world");
    }

    fn m2(&mut self) { // warning: unused self can be removed
        println!("Hello again");
    }
}

Though, this warning should have a possibility to be ommited. And, for example, there are some special cases, one of which @eddyb has pointed me to: if we have a trait which requires method to accept self as a parameter, we don't need to emit such warning:


struct A(u64);
trait Printable {
    fn m1(&self);
}
impl Printable for A {
    fn m1(&self) { // no warning is here
        println!("Printed!");
    }
}

The reason of that is making easier to refactor this code in the future (especially if it is very long and we must read the whole method body just to understand that it does not depend on object at all) and to allow code users to call this method without an object.

If this idea gets approved, I would like to implement it by myself. But I have no idea how to work with it so I would like to get some advices where to look at, in learning purposes. I love rust and I want to help to make it better as much as I can.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the RFC repository's contribution guidance and the Rust examples in this issue. Clarify the proposed warning, how it can be omitted, and the trait-implementation exception before identifying the compiler work and tests needed to demonstrate the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.