clippy: remove needless borrows#1283
Merged
petertseng merged 1 commit intoexercism:mainfrom Jul 7, 2021
petertseng:clippy
Merged
clippy: remove needless borrows#1283petertseng merged 1 commit intoexercism:mainfrom petertseng:clippy
petertseng merged 1 commit intoexercism:mainfrom
petertseng:clippy
Conversation
This was made an error in Clippy recently, so we'll need to fix it if we want CI to work. According to https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow What it does: Checks for address of operations (`&`) that are going to be dereferenced immediately by the compiler. Why is this bad: Suggests that the receiver of the expression borrows the expression. Commentary as it applies to the Rust track: I can get behind the idea that it's better not to mislead the reader of the code about how many levels of reference the called function needs. Most of these stem from a misunderstanding of what expressions were already borrowed. They are listed below. Note carefully inconsistencies introduced in sublist and tournament, and consider whether we have a solution for this inconsistency before approving. The concern would be any confusion it may cause to those learning Rust. It is consistent from the standpoint of the type we're ultimately passing, but it's inconsistent from the standpoint of what the call sites look like. * Dominoes: The input is `&[Domino]`, and check takes `&[Domino]`. calling `check(&input)` passes `&&[Domino]` whereas we should call `check(input)` to pass `&[Domino]` * DOT DSL: `iter` iterates over `&T`. Therefore the `name` is a `&&str` (recall that string literals in Rust produce `&str`) and we are iterating over a `[&str]` producing `&&str`; since `Node::new` takes `&str`, just `name` would be better than `&name`, which would pass `&&&str` * grep: The patterns are string literals such as `"Agamemnon"`. Recall that string literals in Rust produce `&str`, and grep takes `&str`, so no additional `&` should be added. In fact, we should do this for the macro too, even though Clippy does not seem to catch those. * pangram: Recall that string literals in Rust produce `&str`, and is_pangram takes `&str`, so no additional `&` should be added. * sublist: The `v` was declared as `&[u32]`, and `sublist` takes two `&[T]`, so no additional `&` should be added. Inconsistency introduced: `sublist` is called with `&` in all other instances in this file because they are either slice literals or Vec, which do require the `&`. * tournament: Recall that string literals in Rust produce `&str`, and `tally` takes `&str`, so no additional `&` should be added. Inconsistency introduced: The first four cases use a string literal and thus do not require `&`. The next cases use `String` (recall that we decided that this was the best way to show the multi-line strings in #152 (comment)), and therefore require `&`. Consider carefully whether this may be confusing to students that some require `&` and some not.
Member
Author
|
I have determined that if we want to have some consistency in tournament, I originally wanted to avoid multiline string literals having indentation similar to: fn test() {
let scores = "header
line1
line2";
}Perhaps https://doc.rust-lang.org/std/macro.concat.html could be an alternative. |
coriolinus
approved these changes
Jul 7, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This was made an error in Clippy recently, so we'll need to fix it if we
want CI to work.
According to https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
What it does:
Checks for address of operations (
&) that are going to be dereferencedimmediately by the compiler.
Why is this bad:
Suggests that the receiver of the expression borrows the expression.
Commentary as it applies to the Rust track:
I can get behind the idea that it's better not to mislead the reader of
the code about how many levels of reference the called function needs.
Most of these stem from a misunderstanding of what expressions were
already borrowed. They are listed below. Note carefully inconsistencies
introduced in sublist and tournament, and consider whether we have a
solution for this inconsistency before approving. The concern would be
any confusion it may cause to those learning Rust. It is consistent from
the standpoint of the type we're ultimately passing, but it's
inconsistent from the standpoint of what the call sites look like.
&[Domino], and check takes&[Domino].calling
check(&input)passes&&[Domino]whereas we should callcheck(input)to pass&[Domino]iteriterates over&T. Therefore thenameis a&&str(recall that string literals in Rust produce
&str) and we areiterating over a
[&str]producing&&str; sinceNode::newtakes&str, justnamewould be better than&name, which would pass&&&str"Agamemnon". Recallthat string literals in Rust produce
&str, and grep takes&str, sono additional
&should be added. In fact, we should do this for themacro too, even though Clippy does not seem to catch those.
&str, andis_pangram takes
&str, so no additional&should be added.vwas declared as&[u32], andsublisttakes two&[T], so no additional&should be added. Inconsistencyintroduced:
sublistis called with&in all other instances inthis file because they are either slice literals or Vec, which do
require the
&.&str, andtallytakes&str, so no additional&should be added.Inconsistency introduced: The first four cases use a string literal
and thus do not require
&. The next cases useString(recall thatwe decided that this was the best way to show the multi-line strings
in tournament: Put inputs/expectations inline, not in files #152 (comment)),
and therefore require
&. Consider carefully whether this may beconfusing to students that some require
&and some not.