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

Add rc_buffer lint for checking Rc<String> and friends #6044

Merged
merged 5 commits into from
Sep 23, 2020

Conversation

rschoon
Copy link
Contributor

@rschoon rschoon commented Sep 14, 2020

Fixes #2623

This is a bit different from the original PR attempting to implement this type of lint. Rather than linting against converting into the unwanted types, this PR lints against declaring the unwanted type in a struct or function definition.

I'm reasonably happy with what I have here, although I used the fully qualified type names for the Path and OsString suggestions, and I'm not sure if I should have just used the short versions instead, even if they might not have been declared via use.

Also, I don't know if "buffer type" is the best way to put it or not. Alternatively I could call it a "growable type" or "growable buffer type", but I was thinking of PathBuf when I started making the lint.

changelog: Add rc_buffer lint

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @yaahc (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 14, 2020
Copy link
Member

@yaahc yaahc left a comment

Choose a reason for hiding this comment

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

This is a bit different from the original PR attempting to implement this type of lint. Rather than linting against converting into the unwanted types, this PR lints against declaring the unwanted type in a struct or function definition.

👍

I'm reasonably happy with what I have here, although I used the fully qualified type names for the Path and OsString suggestions, and I'm not sure if I should have just used the short versions instead, even if they might not have been declared via use.

I think using fully qualified paths is fine. It can always be changed later if it's causing issues.

CHANGELOG.md Show resolved Hide resolved
clippy_lints/src/types.rs Outdated Show resolved Hide resolved
tests/ui/rc_buffer.rs Show resolved Hide resolved
@yaahc
Copy link
Member

yaahc commented Sep 21, 2020

the changes look great btw, once we fix the dogfood issue I'll be happy to approve this.

We can avoid the data copy again by fixing rustc_ast::ast::LitKind
later.
@yaahc
Copy link
Member

yaahc commented Sep 23, 2020

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 23, 2020

📌 Commit 6c056d3 has been approved by yaahc

@bors
Copy link
Collaborator

bors commented Sep 23, 2020

⌛ Testing commit 6c056d3 with merge e636b88...

@bors
Copy link
Collaborator

bors commented Sep 23, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: yaahc
Pushing e636b88 to master...

@bors bors merged commit e636b88 into rust-lang:master Sep 23, 2020
@rschoon rschoon deleted the rc-buffer branch September 23, 2020 20:35
Fishrock123 added a commit to Fishrock123/surf that referenced this pull request Sep 26, 2020
This new lint popped up overnight, see
rust-lang/rust-clippy#6044

The problem is that we do indeed want this structure.
- The outer Arc allows us to clone in .send() without cloning the array.
- The Vec allows us to add middleware at runtime.
- The inner Arc-s allow us to implement Clone without sharing the vector with the parent.
- We don't use a Mutex around the Vec here because adding a middleware during execution should be an error.
@yoshuawuyts
Copy link
Member

yoshuawuyts commented Sep 26, 2020

We've hit this lint in Surf and are moving to disable it: http-rs/surf#242. We use a similar structure for middleware in Tide as well. This may be an interesting case study for legitimate uses that are disallowed by this lint.

Fishrock123 added a commit to Fishrock123/rust-clippy that referenced this pull request Sep 27, 2020
This didn't display some types properly in the docs due the lack of code formatting.

Also, refs for the caveat:
rust-lang#6044 (comment)
http-rs/surf#242
@Fishrock123
Copy link
Contributor

Fishrock123 commented Sep 27, 2020

So to elaborate more on Tide & Surf's usage:

The structure we have is Arc<Vec<Arc<dyn Middleware>>>, in both tide::Server and surf::Client.

The outer Arc exists to be able to handle things per-request, in both Tide and Surf, in both cases to get around a bunch of lifetime indirection I really don't understand. Both are constructed at runtime, so we need a Vec so people can add middleware (via .with(middleware)). The inner Arc exists due to an inner clone in Surf, and sharing between routes in Tide.

In both cases, the following information from the lint is not true:

    /// While mutating a buffer type would still be possible with Rc::get_mut(), it only
    /// works if there are no additional references yet, which defeats the purpose of
    /// enclosing it in a shared ownership type. Instead, additionally wrapping the inner
    /// type with an interior mutable container (such as RefCell or Mutex) would normally
    /// be used.

This is because the behavior described is useful to both Surf and Tide. Middleware needs to be mutable up-front, but once a Client or Server is executing on a request, messing with the middleware would lead to unexpected (and probably zalgo-esque timing bug) behavior, so having it panic is fine, because that is considered a programmer error. We could use an extra RefCell or Mutex but this brings us extra overhead and does less of what we'd want.

bors added a commit that referenced this pull request Sep 27, 2020
lints: clarify rc_buffer and add caveats

This didn't display some types properly in the docs due the lack of code formatting.

Also, refs for the caveat:
#6044 (comment)
http-rs/surf#242

Fwiw I can't get `cargo test` to run, even on nightly. I get:
```
error[E0463]: can't find crate for `rustc_ast`
```

*Please keep the line below*
changelog: none, nightly
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 6, 2020
Fix LitKind's byte buffer to use refcounted slice

While working on adding a new lint for clippy (see rust-lang/rust-clippy#6044) for avoiding shared ownership of "mutable buffer" types (such as using `Rc<Vec<T>>` instead of `Rc<[T]>`), I noticed a type exported from rustc_ast and used by clippy gets caught by the lint. This PR fixes the exported type.

This PR includes the actual change to clippy too, but I will open a PR directly against clippy for that part (although it will currently fail to build there).
rschoon pushed a commit to rschoon/rust-clippy that referenced this pull request Oct 6, 2020
Fix LitKind's byte buffer to use refcounted slice

While working on adding a new lint for clippy (see rust-lang#6044) for avoiding shared ownership of "mutable buffer" types (such as using `Rc<Vec<T>>` instead of `Rc<[T]>`), I noticed a type exported from rustc_ast and used by clippy gets caught by the lint. This PR fixes the exported type.

This PR includes the actual change to clippy too, but I will open a PR directly against clippy for that part (although it will currently fail to build there).
bors added a commit that referenced this pull request Oct 7, 2020
Downgrade rc_buffer to restriction

I think Arc\<Vec\<T\>\> and Arc\<String\> and similar are a totally reasonable data structure, as observed by others in the comments on [#6044](#6044 (comment)) as well. Doing `Arc::make_mut(&mut self.vec).push(...)` or `Arc::make_mut(&mut self.string).push_str("...")` is a terrific and well performing copy-on-write pattern. Linting this with an enabled-by-default <kbd>performance</kbd> lint strikes me as an unacceptable false positive balance.

As of #6090 the documentation of this lint now contains:

> **Known problems:** This pattern can be desirable ...

which should indicate that we shouldn't be linting against correct, reasonable, well-performing patterns with an enabled-by-default lint.

Mentioning #6044, #6090.
r? `@yaahc,` who reviewed the lint.

---

changelog: Remove rc_buffer from default set of enabled lints
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.

Lint against Rc<String> (and Rc<Vec<T>>)
6 participants