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

Rollup of 10 pull requests #82795

Merged
merged 73 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
f45bdcc
Implement size_hint for BufReader
Xavientois Jan 15, 2021
c3e47d9
Fix implementation to specialize
Xavientois Jan 15, 2021
fa76db3
Use helper trait to follow min_specialization rules
Xavientois Jan 15, 2021
11c49f6
Add missing generic
Xavientois Jan 15, 2021
260a270
Move default to trait definition
Xavientois Jan 15, 2021
5f60a30
Fix incorrect token
Xavientois Jan 15, 2021
eea99f4
Add default keyword for specialization
Xavientois Jan 15, 2021
7e56637
Add back lower_bound as memeber
Xavientois Jan 16, 2021
442de9a
Fix semicolon
Xavientois Jan 16, 2021
1190321
Remove exposing private trait
Xavientois Jan 16, 2021
421b40c
Add dyn for SizeHint cast
Xavientois Jan 16, 2021
265db94
Fix formatting
Xavientois Jan 16, 2021
93870c8
Remove stable annotation
Xavientois Jan 16, 2021
7869371
Remove unnecessary default keyword
Xavientois Jan 16, 2021
c8e0f8a
Use fully qualified syntax to avoid dyn
Xavientois Jan 16, 2021
96255f8
Implement SizeHint trait for BufReader, Emtpy, and Chain
Xavientois Jan 16, 2021
389e638
Add tests for SizeHint implementations
Xavientois Jan 16, 2021
b837f3a
Remove trailing newline
Xavientois Jan 16, 2021
81aba38
Add space for proper indentation
Xavientois Jan 16, 2021
fc9cd4a
Fix formatting on mod
Xavientois Jan 16, 2021
7674ae1
Fix line length format
Xavientois Jan 31, 2021
7b021aa
resolve: Reduce scope of `pub_use_of_private_extern_crate` deprecatio…
petrochenkov Jan 28, 2021
a3db47a
Add suggestion for iterators in iterators
kper Feb 9, 2021
040735c
First version of noop-lint
rylev Jan 5, 2021
f49ed7a
Add tests and support two more noop methods
rylev Jan 5, 2021
a6d926d
Fix tests
rylev Jan 5, 2021
618c395
Bless test where order of error message changed
rylev Jan 6, 2021
3a86184
Fix ui-full-deps suite
rylev Jan 6, 2021
ee65416
Fix core tests
rylev Jan 6, 2021
d3b49c2
Only allow new lint when not bootstrapping - since beta doesn't know …
rylev Jan 7, 2021
c5ff54c
Fix std tests
rylev Jan 7, 2021
217c886
Improve warning
rylev Jan 8, 2021
16c4afb
Fix tidy errors
rylev Jan 8, 2021
95e330b
Update error message
rylev Jan 11, 2021
316e9db
Fix tidy error
rylev Jan 11, 2021
49f32e0
Improve error messages
rylev Jan 12, 2021
e48670c
Increase accuracy of lint trigger
estebank Jan 13, 2021
055db16
Clean up code rightward drift
estebank Jan 13, 2021
4be7052
Allow noop_method_call in clippy ui test
rylev Jan 18, 2021
6bf6652
Move unrelated ui test back to what it was before
rylev Feb 10, 2021
da3995f
Remove lint pass on borrow and deref
rylev Feb 16, 2021
1999a31
Fix borrow and deref
rylev Feb 16, 2021
25637b2
Warn in doc test
rylev Feb 17, 2021
9425e30
Avoid unnecessary Vec construction in BufReader
calebsander Dec 28, 2020
66a2606
Increment self.len in specialized ZipImpl to avoid underflow in size_…
SkiFire13 Feb 19, 2021
8b9ac4d
Add test for underflow in specialized Zip's size_hint
SkiFire13 Feb 19, 2021
aeb4ea7
Remove useless comparison since now self.index <= self.len is an inva…
SkiFire13 Feb 19, 2021
09cbcdc
Add BTreeMap::try_insert and btree_map::OccupiedError.
m-ou-se Mar 4, 2021
f6fe24a
Add HashMap::try_insert and hash_map::OccupiedError.
m-ou-se Mar 4, 2021
69d95e2
Improve Debug implementations of OccupiedError.
m-ou-se Mar 4, 2021
d85d82a
Implement Error for OccupiedError.
m-ou-se Mar 4, 2021
da01455
Ignore file length tidy warning in hash/map.rs.
m-ou-se Mar 4, 2021
1aedb4c
Remove unnecessary bound from HashMap::try_insert.
m-ou-se Mar 4, 2021
eddd4f0
Add tracking issue for map_try_insert.
m-ou-se Mar 4, 2021
eb18746
Add assert_matches!(expr, pat).
m-ou-se Mar 4, 2021
cfce60e
Allow for multiple patterns and a guard in assert_matches.
m-ou-se Mar 4, 2021
0a8e401
Add debug_assert_matches macro.
m-ou-se Mar 4, 2021
5bd1204
Fix assert_matches doc examples.
m-ou-se Mar 4, 2021
a5951d4
Add diagnostic item to `Default` trait
mgacek8 Mar 4, 2021
58d6f80
Fix comment with path to `symbols!` macro
mgacek8 Mar 4, 2021
f223aff
Don't consume the expression in assert_matches!()'s failure case.
m-ou-se Mar 4, 2021
80fcdef
Add tracking issue for assert_matches.
m-ou-se Mar 4, 2021
ad91915
Remove unused code from main.js
notriddle Mar 5, 2021
e6a6df5
Rollup merge of #80723 - rylev:noop-lint-pass, r=estebank
m-ou-se Mar 5, 2021
ec2619c
Rollup merge of #80763 - petrochenkov:pubusecrate, r=estebank
m-ou-se Mar 5, 2021
6013811
Rollup merge of #81136 - Xavientois:io_reader_size_hint, r=cramertj
m-ou-se Mar 5, 2021
20887b7
Rollup merge of #81939 - kper:fixing-81584-allocate-in-iter, r=davidtwco
m-ou-se Mar 5, 2021
ee796c6
Rollup merge of #82289 - SkiFire13:fix-issue-82282, r=m-ou-se
m-ou-se Mar 5, 2021
68f2934
Rollup merge of #82728 - calebsander:refactor/bufreader-buf, r=m-ou-se
m-ou-se Mar 5, 2021
232caad
Rollup merge of #82764 - m-ou-se:map-try-insert, r=Amanieu
m-ou-se Mar 5, 2021
04045cc
Rollup merge of #82770 - m-ou-se:assert-match, r=joshtriplett
m-ou-se Mar 5, 2021
2cd1f79
Rollup merge of #82773 - mgacek8:feature/add_diagnostic_item_to_Defau…
m-ou-se Mar 5, 2021
16518e7
Rollup merge of #82787 - notriddle:main-js-cleanup, r=GuillaumeGomez
m-ou-se Mar 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix borrow and deref
  • Loading branch information
rylev committed Mar 3, 2021
commit 1999a3147f5ab65cd556d45e631be5c18fbaebf4
18 changes: 6 additions & 12 deletions compiler/rustc_lint/src/noop_method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare_lint! {
///
/// ```rust
/// # #![allow(unused)]
/// #![deny(noop_method_call)]
/// struct Foo;
/// let foo = &Foo;
/// let clone: &Foo = foo.clone();
Expand All @@ -30,7 +31,7 @@ declare_lint! {
/// calling `clone` on a `&T` where `T` does not implement clone, actually doesn't do anything
/// as references are copy. This lint detects these calls and warns the user about them.
pub NOOP_METHOD_CALL,
Warn,
Allow,
"detects the use of well-known noop methods"
}

Expand All @@ -50,7 +51,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
// Check that we're dealing with a trait method for one of the traits we care about.
Some(trait_id)
if [sym::Clone].iter().any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
if [sym::Clone, sym::Deref, sym::Borrow]
.iter()
.any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
{
(trait_id, did)
}
Expand All @@ -71,20 +74,11 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
_ => return,
};
// (Re)check that it implements the noop diagnostic.
for (s, peel_ref) in [(sym::noop_method_clone, false)].iter() {
for s in [sym::noop_method_clone, sym::noop_method_deref, sym::noop_method_borrow].iter() {
if cx.tcx.is_diagnostic_item(*s, i.def_id()) {
let method = &call.ident.name;
let receiver = &elements[0];
let receiver_ty = cx.typeck_results().expr_ty(receiver);
let receiver_ty = match receiver_ty.kind() {
// Remove one borrow from the receiver if appropriate to positively verify that
// the receiver `&self` type and the return type are the same, depending on the
// involved trait being checked.
ty::Ref(_, ty, _) if *peel_ref => ty,
// When it comes to `Clone` we need to check the `receiver_ty` directly.
// FIXME: we must come up with a better strategy for this.
_ => receiver_ty,
};
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
if receiver_ty != expr_ty {
// This lint will only trigger if the receiver type and resulting expression \
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ symbols! {
Decodable,
Decoder,
Default,
Deref,
Encodable,
Encoder,
Eq,
Expand Down Expand Up @@ -790,7 +791,9 @@ symbols! {
none_error,
nontemporal_store,
nontrapping_dash_fptoint: "nontrapping-fptoint",
noop_method_borrow,
noop_method_clone,
noop_method_deref,
noreturn,
nostack,
not,
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
/// [`HashMap<K, V>`]: ../../std/collections/struct.HashMap.html
/// [`String`]: ../../std/string/struct.String.html
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "Borrow"]
pub trait Borrow<Borrowed: ?Sized> {
/// Immutably borrows from an owned value.
///
Expand Down Expand Up @@ -205,6 +206,7 @@ pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Borrow<T> for T {
#[rustc_diagnostic_item = "noop_method_borrow"]
fn borrow(&self) -> &T {
self
}
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/ops/deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#[doc(alias = "*")]
#[doc(alias = "&*")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "Deref"]
pub trait Deref {
/// The resulting type after dereferencing.
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -78,6 +79,7 @@ pub trait Deref {
impl<T: ?Sized> Deref for &T {
type Target = T;

#[rustc_diagnostic_item = "noop_method_deref"]
fn deref(&self) -> &T {
*self
}
Expand Down
2 changes: 0 additions & 2 deletions library/core/tests/clone.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![cfg_attr(not(bootstrap), allow(noop_method_call))]

#[test]
fn test_borrowed_clone() {
let x = 5;
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-11820.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// run-pass
// pretty-expanded FIXME #23616

#![allow(noop_method_call)]

struct NoClone;

fn main() {
Expand Down
30 changes: 25 additions & 5 deletions src/test/ui/lint/noop-method-call.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// check-pass

#![allow(unused)]
#![warn(noop_method_call)]

struct NonCloneType<T>(T);
use std::borrow::Borrow;
use std::ops::Deref;

struct PlainType<T>(T);

#[derive(Clone)]
struct CloneType<T>(T);

fn main() {
let non_clone_type_ref = &NonCloneType(1u32);
let non_clone_type_ref_clone: &NonCloneType<u32> = non_clone_type_ref.clone();
let non_clone_type_ref = &PlainType(1u32);
let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
//~^ WARNING call to `.clone()` on a reference in this situation does nothing

let clone_type_ref = &CloneType(1u32);
Expand All @@ -20,15 +24,31 @@ fn main() {
let clone_type_ref = &&CloneType(1u32);
let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();

let non_deref_type = &PlainType(1u32);
let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
//~^ WARNING call to `.deref()` on a reference in this situation does nothing

// Dereferencing a &&T does not warn since it has collapsed the double reference
let non_deref_type = &&PlainType(1u32);
let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();

let non_borrow_type = &PlainType(1u32);
let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
//~^ WARNING call to `.borrow()` on a reference in this situation does nothing

// Borrowing a &&T does not warn since it has collapsed the double reference
let non_borrow_type = &&PlainType(1u32);
let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();

let xs = ["a", "b", "c"];
let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // ok, but could use `*x` instead
}

fn generic<T>(non_clone_type: &NonCloneType<T>) {
fn generic<T>(non_clone_type: &PlainType<T>) {
non_clone_type.clone();
}

fn non_generic(non_clone_type: &NonCloneType<u32>) {
fn non_generic(non_clone_type: &PlainType<u32>) {
non_clone_type.clone();
//~^ WARNING call to `.clone()` on a reference in this situation does nothing
}
36 changes: 28 additions & 8 deletions src/test/ui/lint/noop-method-call.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:12:74
--> $DIR/noop-method-call.rs:16:71
|
LL | let non_clone_type_ref_clone: &NonCloneType<u32> = non_clone_type_ref.clone();
| ^^^^^^^^ unnecessary method call
LL | let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
| ^^^^^^^^ unnecessary method call
|
= note: `#[warn(noop_method_call)]` on by default
= note: the type `&NonCloneType<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
note: the lint level is defined here
--> $DIR/noop-method-call.rs:4:9
|
LL | #![warn(noop_method_call)]
| ^^^^^^^^^^^^^^^^
= note: the type `&PlainType<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed

warning: call to `.deref()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:28:63
|
LL | let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
| ^^^^^^^^ unnecessary method call
|
= note: the type `&PlainType<u32>` which `deref` is being called on is the same as the type returned from `deref`, so the method call does not do anything and can be removed

warning: call to `.borrow()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:36:66
|
LL | let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
| ^^^^^^^^^ unnecessary method call
|
= note: the type `&PlainType<u32>` which `borrow` is being called on is the same as the type returned from `borrow`, so the method call does not do anything and can be removed

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:32:19
--> $DIR/noop-method-call.rs:52:19
|
LL | non_clone_type.clone();
| ^^^^^^^^ unnecessary method call
|
= note: the type `&NonCloneType<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
= note: the type `&PlainType<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed

warning: 2 warnings emitted
warning: 4 warnings emitted

1 change: 0 additions & 1 deletion src/test/ui/underscore-imports/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ mod y {

pub fn main() {
use x::*;
#[allow(noop_method_call)]
(&0).deref();
}
1 change: 0 additions & 1 deletion src/test/ui/underscore-imports/macro-expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// check-pass

#![feature(decl_macro, rustc_attrs)]
#![allow(noop_method_call)]

mod x {
pub use std::ops::Not as _;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/unnecessary_clone.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// does not test any rustfixable lints

#![warn(clippy::clone_on_ref_ptr)]
#![allow(unused, noop_method_call, clippy::redundant_clone, clippy::unnecessary_wraps)]
#![allow(unused, clippy::redundant_clone, clippy::unnecessary_wraps)]

use std::cell::RefCell;
use std::rc::{self, Rc};
Expand Down