Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

missing_doc_code_examples lint complains about foreign trait implementations #76450

Closed
skreborn opened this issue Sep 7, 2020 · 19 comments · Fixed by #90098
Closed

missing_doc_code_examples lint complains about foreign trait implementations #76450

skreborn opened this issue Sep 7, 2020 · 19 comments · Fixed by #90098
Labels
A-doc-coverage Area: Calculating how much of a crate has documentation A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. requires-nightly This issue requires a nightly compiler in some way. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@skreborn
Copy link

skreborn commented Sep 7, 2020

The missing_doc_code_examples lint seems to be erroneously complaining about missing documentation examples where local types implement foreign traits (both from 3rd party crates and the standard library).

The example below should pass all checks as far as I can tell:

/// Foo.
pub struct Foo {
  /// Bar.
  pub bar: i32,
  /// Baz.
  pub baz: i32,
}

impl Neg for Foo {
  type Output = Self;

  #[inline]
  fn neg(self) -> Self::Output {
    Self::Output {
      bar: -self.bar,
      baz: -self.baz,
    }
  }
}

However, running cargo doc results in the following warnings:

warning: missing code example in this documentation
   --> foo.rs:9:1
    |
    | / impl Neg for Foo {
    | |   type Output = Self;
    | |
    | |   #[inline]
...   |
    | |   }
    | | }
    | |_^

warning: missing code example in this documentation
   --> foo.rs:13:3
    |
    | /   fn neg(self) -> Self::Output {
    | |     Self::Output {
    | |       bar: -self.bar,
    | |       baz: -self.baz,
    | |     }
    | |   }
    | |___^

Meta

rustc --version --verbose:

rustc 1.48.0-nightly (73dc675b9 2020-09-06)
binary: rustc
commit-hash: 73dc675b9437c2a51a975a9f58cc66f05463c351
commit-date: 2020-09-06
host: x86_64-pc-windows-msvc
release: 1.48.0-nightly
LLVM version: 11.0
@skreborn skreborn added the C-bug Category: This is a bug. label Sep 7, 2020
@jyn514 jyn514 added requires-nightly This issue requires a nightly compiler in some way. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Sep 7, 2020
@jyn514
Copy link
Member

jyn514 commented Sep 7, 2020

cc @GuillaumeGomez

@jyn514 jyn514 added A-doc-coverage Area: Calculating how much of a crate has documentation A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Sep 7, 2020
@GuillaumeGomez
Copy link
Member

I think the current lint behavior is correct. The issue here is on the core lib side. I opened #76568 to fix it.

@jyn514
Copy link
Member

jyn514 commented Sep 10, 2020

Maybe the lint shouldn't warn when implementing a foreign trait? By the same rationale as #56922.

@GuillaumeGomez
Copy link
Member

Hmmm... I have shared feelings here: if a trait doesn't provided documentation "by default", doesn't it make sense to complain about it on the implementors?

@jyn514
Copy link
Member

jyn514 commented Sep 10, 2020

I guess if you can add docs on the implementation it makes sense - you have a way to fix it.

@GuillaumeGomez
Copy link
Member

That's mainly my position yes. :)

@skreborn
Copy link
Author

I guess if you can add docs on the implementation it makes sense - you have a way to fix it.

Unfortunately, that drags with itself more issues. First of all, I have to implement said examples on all of the implementations I write. Second, I have to copy (and correctly maintain said copy) the original example-less documentation onto every instance so that it's not lost by the language server and/or rustdoc.

While this isn't entirely impossible, it's a huge chore, especially when you implement multiple traits on tens of types at a time. While std could get its issues fixed, that hardly helps us with the rest of the crate ecosystem. As of now, I have about 300 warnings, and actually useful information just drowns in that flood.

@GuillaumeGomez
Copy link
Member

You can disable the lint on a specific implementation in the worst case. However, the goal is to enforce more documentation through the ecosystem. If users start to complain about it to the crates' maintainers, I'm pretty sure the situation will improve pretty quickly.

@jyn514
Copy link
Member

jyn514 commented Sep 10, 2020

However, the goal is to enforce more documentation through the ecosystem. If users start to complain about it to the crates' maintainers, I'm pretty sure the situation will improve pretty quickly

I don't really like this approach - it should be fixable by you, not by bugging upstream maintainers. @Manishearth was saying something like that for #74563 I think.

@GuillaumeGomez
Copy link
Member

You can fix it on your side by adding the examples yourself in your implementation. So on that on point, even if not the best, it's still possible.

@skreborn
Copy link
Author

skreborn commented Sep 10, 2020

I suppose in the worst case, I could fork the upstream project, add examples, and hope that it either gets merged or the developer doesn't cause too many conflicts so I can keep it up-to-date.

But even then, it takes a lot of back-and-forth, a lot of time and effort on both ends to get from my linter complaining about missing examples to a new crate release that fixes it - even if everything goes quick and smooth.

There should at least be a way for me to temporarily ignore warnings coming from (or rather because of) 3rd party and standard library crates, so that - at the minimum - I can fix my own linting issues without having to comb through thousands of lines of all too similar lines of warnings every time I generate documentation for testing purposes.

@jyn514
Copy link
Member

jyn514 commented Sep 10, 2020

There should at least be a way for me to temporarily ignore warnings

#[allow(missing_doc_code_examples)] on the implementation?

@GuillaumeGomez
Copy link
Member

Like I said, you can ignore the lint on the implementations, or directly on a module if it only contains such cases. Adding a #[allow(...)] doesn't seem to be such a burden...

@skreborn
Copy link
Author

Let it be known that I was today days old when I learned that you can apply #[allow(missing_doc_code_examples)] to a single impl block. I suppose that will have to suffice for now.

@Manishearth
Copy link
Member

I strongly feel that we should not warn on foreign trait implementations here. Firstly, as @jyn514 said, a lint should be fixable by you. Yes, you can add examples yourself, but that isn't actually the real fix here in most cases. In most cases trait implementations are lightweight and you rarely document them, so you're not actually giving the user a "fix", you're asking them to do something they don't actually want to shut the lint up. That's not good, that is not the actual fix, the actual fix is asking people to fix it upstream, and we should not lint that downstream.

"The lint can be ignored" is not good enough for normal situations -- if it was some weird confluence of situations, then, sure, but a foreign trait impl is an extremely normal thing to do.

tmandry added a commit to tmandry/rust that referenced this issue Sep 10, 2020
…, r=jyn514

Add missing examples on core traits' method

Linked to rust-lang#76450.

r? @jyn514
@GuillaumeGomez
Copy link
Member

Coming back on this after a while and I now agree with this issue: we shouldn't lint on foreign trait implementations. I'll send a fix in the following days.

@GuillaumeGomez
Copy link
Member

I just tried locally and it seems like this issue has already been fixed. Closing it then.

@jyn514
Copy link
Member

jyn514 commented Oct 20, 2021

@GuillaumeGomez we should still test that it behaves properly

@jyn514 jyn514 reopened this Oct 20, 2021
@jyn514 jyn514 added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Oct 20, 2021
@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Oct 20, 2021

I'll send a test if there isn't already one then.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 20, 2021
…l-missing-doc-code-examples, r=jyn514

Add test to ensure that the missing_doc_code_examples is not triggered on foreign trait implementations

Fixes rust-lang#76450.

r? `@jyn514`
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Oct 20, 2021
…l-missing-doc-code-examples, r=jyn514

Add test to ensure that the missing_doc_code_examples is not triggered on foreign trait implementations

Fixes rust-lang#76450.

r? ``@jyn514``
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 21, 2021
…l-missing-doc-code-examples, r=jyn514

Add test to ensure that the missing_doc_code_examples is not triggered on foreign trait implementations

Fixes rust-lang#76450.

r? ```@jyn514```
@bors bors closed this as completed in 68a5680 Oct 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-doc-coverage Area: Calculating how much of a crate has documentation A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. requires-nightly This issue requires a nightly compiler in some way. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
4 participants