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

#[must_use] for functions #43728

Merged
merged 2 commits into from
Aug 9, 2017
Merged

#[must_use] for functions #43728

merged 2 commits into from
Aug 9, 2017

Conversation

zackmdavis
Copy link
Member

@zackmdavis zackmdavis commented Aug 8, 2017

This implements RFC 1940.

The RFC and discussion thereof seem to suggest that tagging PartialEq::eq and friends as #[must_use] would automatically lint for unused comparisons, but it doesn't work out that way (at least the way I've implemented it): unused .eq method calls get linted, but not == expressions. (The lint operates on the HIR, which sees binary operations as their own thing, even if they ultimately just call .eq &c..)

What do you think??

Resolves #43302.

@rust-highfive
Copy link
Collaborator

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@zackmdavis
Copy link
Member Author

Hm, maybe the feature gate is too conservative (the RFC and discussion don't seem to lean that way)?

@zackmdavis
Copy link
Member Author

(force-pushed version without feature gate)

@arielb1
Copy link
Contributor

arielb1 commented Aug 8, 2017

r? @eddyb

@arielb1 arielb1 assigned eddyb and unassigned nikomatsakis Aug 8, 2017
@arielb1 arielb1 added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 8, 2017
@eddyb
Copy link
Member

eddyb commented Aug 8, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Aug 8, 2017

📌 Commit 88b686e has been approved by eddyb

The return value of a function annotated with `must_use`, must be used.

This is in the matter of rust-lang#43302.
Note that this doesn't actually give us warnings on `a == b;` and the like, as
some observers may have hoped.

This is in the matter of rust-lang#43302.
@zackmdavis
Copy link
Member Author

Wait, sorry, I needed to fix the commit message of what is now 3645b06 (it still mentioned the feature gate). (The eternal shame of a factually untrue statement blotting the Git history forever is not something I want on my conscience.)

@eddyb reapprove?

@eddyb
Copy link
Member

eddyb commented Aug 8, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Aug 8, 2017

📌 Commit f5ac228 has been approved by eddyb

@@ -625,6 +627,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
/// let result = std::f64::NAN.partial_cmp(&1.0);
/// assert_eq!(result, None);
/// ```
#[must_use]
Copy link
Member

Choose a reason for hiding this comment

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

This method is the only newly-annotated one that's not an operator. Intentional?

(I'm by no means against it, and see no situation where it being unused would be good, but I recall the RFC intentionally leaving it off most places for now.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Intentional; it felt like "part of the set", even if it's not really an operator.

@bors
Copy link
Contributor

bors commented Aug 9, 2017

⌛ Testing commit f5ac228 with merge 3f977ba...

bors added a commit that referenced this pull request Aug 9, 2017
#[must_use] for functions

This implements [RFC 1940](rust-lang/rfcs#1940).

The RFC and discussion thereof seem to suggest that tagging `PartialEq::eq` and friends as `#[must_use]` would automatically lint for unused comparisons, but it doesn't work out that way (at least the way I've implemented it): unused `.eq` method calls get linted, but not `==` expressions. (The lint operates on the HIR, which sees binary operations as their own thing, even if they ultimately just call `.eq` _&c._.)

What do _you_ think??

Resolves #43302.
@bors
Copy link
Contributor

bors commented Aug 9, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: eddyb
Pushing 3f977ba to master...

@bors bors merged commit f5ac228 into rust-lang:master Aug 9, 2017
@crumblingstatue
Copy link
Contributor

crumblingstatue commented Aug 9, 2017

unused .eq method calls get linted, but not == expressions

I just want to point out that the main motivation for reopening this RFC was exactly to lint against unused ==.

@alexcrichton
Copy link
Member

Thanks for the PR here @zackmdavis!

I think, though, that the libs team may not have gotten a chance to sign off on the standard-library related changes here. I'll bring this up in the next libs triage to make sure we're on board with it, but if we decide to be more conservative for now, would you be up for backing these out temporarily?

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Aug 26, 2017
…, r=alexcrichton

feature-gate #[must_use] for functions as `fn_must_use`

@eddyb I [was](rust-lang#43728 (comment)) [dithering](rust-lang#43728 (comment)) on this, but [your comment](rust-lang#43302 (comment)) makes it sound like we do want a feature gate for this? Please advise.

r? @eddyb
zackmdavis added a commit to zackmdavis/rust that referenced this pull request Aug 28, 2017
This, as rust-lang#43813, is due to the author of rust-lang#43728 (specifically,
3645b06) being a damnably contemptible fool. Before this entire
fiasco, we would return early from the unusedness late lints pass if
the type of the expression within the `hir::StmtSemi` was `!`, `()`,
or a boolean: these types would never get to the point of being marked
as unused results. That is, until the dunce who somehow (!?) came to
be trusted with the plum responsibility of implementing RFC
1940 (`#[must_use]` for functions) went and fouled everything up,
removing the early returns based on the (stupid) thought that there
would be no harm in it, since we would need to continue to check these
types being returned from must_use functions (which was true for the
booleans, at least). But there was harm—harm that any
quarter-way-competent programmer would have surely forseen! For after
the new functional-must-use checks, there was nothing to stop the
previously-returned-early types from falling through to be marked by
the unused-results lint!—a monumentally idiotic error that has cost
the project tens of precious developer- and reviewer-minutes dealing
with the fallout here and in rust-lang#43813.

If 3645b06 is representative of the standard of craftsmanship the
rising generation of software engineers holds themselves to, I weep
for the future of our technological civilization.

Resolves rust-lang#44119.
zackmdavis added a commit to zackmdavis/rust that referenced this pull request Sep 22, 2017
Although RFC 1940 is about annotating functions with `#[must_use]`, a
key part of the motivation was linting unused equality operators.

(See
https://github.com/rust-lang/rfcs/pull/1812#issuecomment-265695898—it
seems to have not been clear to discussants at the time that marking the
comparison methods as `must_use` would not give us the lints on
comparison operators, at least in (what the present author understood
as) the most straightforward implementation, as landed in rust-lang#43728
(3645b06).)

To rectify the situation, we here lint unused comparison operators as
part of the unused-must-use lint (feature gated by the `fn_must_use`
feature flag, which now arguably becomes a slight (tolerable in the
opinion of the present author) misnomer).

This is in the matter of rust-lang#43302.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Sep 24, 2017
add comparison operators to must-use lint (under `fn_must_use` feature)

Although RFC 1940 is about annotating functions with `#[must_use]`, a
key part of the motivation was linting unused equality operators.

(See
https://github.com/rust-lang/rfcs/pull/1812#issuecomment-265695898—it
seems to have not been clear to discussants at the time that marking the
comparison methods as `must_use` would not give us the lints on
comparison operators, at least in (what the present author understood
as) the most straightforward implementation, as landed in rust-lang#43728
(3645b06).)

To rectify the situation, we here lint unused comparison operators as
part of the unused-must-use lint (feature gated by the `fn_must_use`
feature flag, which now arguably becomes a slight (tolerable in the
opinion of the present author) misnomer).

This is in the matter of rust-lang#43302.

cc @crumblingstatue
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Nov 3, 2017
Changelog:
Version 1.21.0 (2017-10-12)
==========================

Language
--------
- [You can now use static references for literals.][43838]
  Example:
  ```rust
  fn main() {
      let x: &'static u32 = &0;
  }
  ```
- [Relaxed path syntax. Optional `::` before `<` is now allowed in all contexts.][43540]
  Example:
  ```rust
  my_macro!(Vec<i32>::new); // Always worked
  my_macro!(Vec::<i32>::new); // Now works
  ```

Compiler
--------
- [Upgraded jemalloc to 4.5.0][43911]
- [Enabled unwinding panics on Redox][43917]
- [Now runs LLVM in parallel during translation phase.][43506]
  This should reduce peak memory usage.

Libraries
---------
- [Generate builtin impls for `Clone` for all arrays and tuples that
  are `T: Clone`][43690]
- [`Stdin`, `Stdout`, and `Stderr` now implement `AsRawFd`.][43459]
- [`Rc` and `Arc` now implement `From<&[T]> where T: Clone`, `From<str>`,
  `From<String>`, `From<Box<T>> where T: ?Sized`, and `From<Vec<T>>`.][42565]

Stabilized APIs
---------------

[`std::mem::discriminant`]

Cargo
-----
- [You can now call `cargo install` with multiple package names][cargo/4216]
- [Cargo commands inside a virtual workspace will now implicitly
  pass `--all`][cargo/4335]
- [Added a `[patch]` section to `Cargo.toml` to handle
  prepublication dependencies][cargo/4123] [RFC 1969]
- [`include` & `exclude` fields in `Cargo.toml` now accept gitignore
  like patterns][cargo/4270]
- [Added the `--all-targets` option][cargo/4400]
- [Using required dependencies as a feature is now deprecated and emits
  a warning][cargo/4364]


Misc
----
- [Cargo docs are moving][43916]
  to [doc.rust-lang.org/cargo](https://doc.rust-lang.org/cargo)
- [The rustdoc book is now available][43863]
  at [doc.rust-lang.org/rustdoc](https://doc.rust-lang.org/rustdoc)
- [Added a preview of RLS has been made available through rustup][44204]
  Install with `rustup component add rls-preview`
- [`std::os` documentation for Unix, Linux, and Windows now appears on doc.rust-lang.org][43348]
  Previously only showed `std::os::unix`.

Compatibility Notes
-------------------
- [Changes in method matching against higher-ranked types][43880] This may cause
  breakage in subtyping corner cases. [A more in-depth explanation is available.][info/43880]
- [rustc's JSON error output's byte position start at top of file.][42973]
  Was previously relative to the rustc's internal `CodeMap` struct which
  required the unstable library `libsyntax` to correctly use.
- [`unused_results` lint no longer ignores booleans][43728]

[42565]: rust-lang/rust#42565
[42973]: rust-lang/rust#42973
[43348]: rust-lang/rust#43348
[43459]: rust-lang/rust#43459
[43506]: rust-lang/rust#43506
[43540]: rust-lang/rust#43540
[43690]: rust-lang/rust#43690
[43728]: rust-lang/rust#43728
[43838]: rust-lang/rust#43838
[43863]: rust-lang/rust#43863
[43880]: rust-lang/rust#43880
[43911]: rust-lang/rust#43911
[43916]: rust-lang/rust#43916
[43917]: rust-lang/rust#43917
[44204]: rust-lang/rust#44204
[cargo/4123]: rust-lang/cargo#4123
[cargo/4216]: rust-lang/cargo#4216
[cargo/4270]: rust-lang/cargo#4270
[cargo/4335]: rust-lang/cargo#4335
[cargo/4364]: rust-lang/cargo#4364
[cargo/4400]: rust-lang/cargo#4400
[RFC 1969]: rust-lang/rfcs#1969
[info/43880]: rust-lang/rust#44224 (comment)
[`std::mem::discriminant`]: https://doc.rust-lang.org/std/mem/fn.discriminant.html
@zackmdavis zackmdavis deleted the fnused branch January 13, 2018 07:43
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.

9 participants