rust-lang / rust-lang/rust-analyzer

Incorrect Codelens Function Reference Count

Open
#15,051 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

Summary

Codelens reference count for a function is incorrect when #[post("/hello")] attribute is used. Clicking on the codelens reference count link (opening the expanded view) shows a smaller number of references and yet still incorrect. Removing the post attribute shows the correct reference count.

Information

OS / VSCode: win11/vscode1.79.1(user setup)
rust-analyzer version: 0.3.1549-standalone (d567091f4 2023-06-11)
rustc: 1.70.0 (90c541806 2023-05-31)

Link to the repository that reproduces this issue: https://github.com/jpnws/example

Before proceeding, enable Rust-analyzer > Lens > References > Method.

image

In the end, you will see the following Rust workspace folder structure.

image

Dependencies: just actix-web and tokio.

Steps to reproduce

  1. Create a folder called example and create example/Cargo.toml file.
  2. In example/Cargo.toml, write:
[workspace]
members = ["main", "lib"]
  1. Create example/main/Cargo.toml and example/main/src/main.rs.
  2. In example/main/Cargo.toml, write:
[package]
name = "main"
version = "0.1.0"
edition = "2021"

[dependencies]
actix-web = "4"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
lib = { path = "../lib" }
  1. In example/main/src/main.rs, write:
use actix_web::{web, App, HttpResponse, HttpServer};

#[tokio::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(HttpResponse::Ok))
            .service(lib::hello)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}
  1. Create example/lib/Cargo.toml and example/lib/src/lib.rs.
  2. In example/lib/Cargo.toml, write:
[package]
name = "lib"
version = "0.1.0"
edition = "2021"

[dependencies]
actix-web = "4"
  1. In example/lib/src/lib.rs, write:
use actix_web::{post, HttpRequest};

#[post("/hello")]
pub async fn hello(_req: HttpRequest) -> String {
    "Hello".to_owned()
}
  1. In example folder, run cargo build.
  2. Open example/lib/src/lib.rs and observe the codelens reference count above the hello function.

Expected

The number of references to the lib::hello function should be 1.

Actual

The numer of references to the lib::hello function is 6.

image

Thoughts

  • Clicking on the codelens "6 references" link shows 2 references in the expanded view, one in example/main/src/main.rs and one to the function definition itself. It is odd to see that the expanded view shows 2 references when it claimed the function has 6. Also, including the function itself as a reference seems odd.

image

  • After removing #[post("/hello")] attribute, the codelens shows 1 reference. Clicking it shows the correct reference: lib::hello in example/main/src/main.rs.

image

image

Contributor guide

Open the contributing guide

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

Reproduce the workspace from main/src/main.rs, lib/src/lib.rs, and the two Cargo.toml files, then run cargo build and inspect the references codelens on lib::hello with and without #[post("/hello")]. Done means the attributed function reports one reference and the expanded view lists only its use in main/src/main.rs, without counting the definition.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, vscode
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.