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

Fix the bugs of manual_memcpy, simplify the suggestion and refactor it #5536

Merged
merged 15 commits into from
May 2, 2020

Conversation

rail-rain
Copy link
Contributor

While I’m working on the long procrastinated work to expand manual_memcpy(#1670), I found a few minor bugs and probably unidiomatic or old coding style. There is a brief explanation of changes to the behaviour this PR will make below. And, I have a questoin: do I need to add tests for the first and second fixed bugs? I thought it might be too rare cases to include the tests for those. I added for the last one though.

  • Bug fix

    • It negates resulted offsets (src/dst_offset) when offset is subtraction by 0. This PR will remove any subtraction by 0 as a part of minification.

      for i in 0..5 {
          dst[i - 0] = src[i];
      }
       warning: it looks like you're manually copying between slices
         --> src/main.rs:2:14
          |
      LL  |     for i in 0..5 {
      -   |              ^^^^ help: try replacing the loop by: `dst[..-5].clone_from_slice(&src[..5])`
      +   |              ^^^^ help: try replacing the loop by: `dst[..5].clone_from_slice(&src[..5])`
          |
    • It prints RangeTo or RangeFull when both of end and offset are 0, which have different meaning. This PR will print 0. I could reject the cases end is 0, but I thought I won’t catch other cases reverse_range_loop will trigger, and it’s over to catch every such cases.

      for i in 0..0 {
          dst[i] = src[i];
      }
       warning: it looks like you're manually copying between slices
         --> src/main.rs:2:14
          |
       LL |     for i in 0..0 {
      -   |              ^^^^ help: try replacing the loop by: `dst.clone_from_slice(&src[..])`
      +   |              ^^^^ help: try replacing the loop by: `dst[..0].clone_from_slice(&src[..0])`
          |
    • it prints four dots when end is None. This PR will ignore any for loops without end because a for loop that takes RangeFrom as its argument and contains indexing without the statements or the expressions that end loops such as break will definitely panic, and manual_memcpy should ignore the loops with such control flow.

      fn manual_copy(src: &[u32], dst: &mut [u32]) {
          for i in 0.. {
              dst[i] = src[i];
          }
      }
      -warning: it looks like you're manually copying between slices
      -  --> src/main.rs:2:14
      -   |
      -LL |     for i in 0.. {
      -   |              ^^^ help: try replacing the loop by: `dst[....].clone_from_slice(&src[....])`
      -   |
  • Simplification of the suggestion

changelog: fixed the bugs of manual_memcpy and also simplify the suggestion.

@phansch phansch added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 28, 2020
@phansch phansch self-requested a review April 28, 2020 12:09
@phansch
Copy link
Member

phansch commented Apr 28, 2020

Thanks for splitting up the commits as they are, that makes reviewing significantly more easy 👍 I'll try and review this by the end of the day.

@flip1995
Copy link
Member

For your question about the tests: You already have them in the PR description, why not just throw them in the test file, with the short comments you wrote, what they are supposed to test? :)

Copy link
Member

@phansch phansch left a comment

Choose a reason for hiding this comment

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

changes LGTM but the 2 tests would indeed be nice to have.

@phansch phansch added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Apr 28, 2020
@rail-rain
Copy link
Contributor Author

Thanks you for your reviews. I've added the 2 tests.

clippy_lints/src/loops.rs Show resolved Hide resolved
@flip1995 flip1995 requested a review from phansch April 30, 2020 20:04
@phansch
Copy link
Member

phansch commented May 2, 2020

@bors r+

@bors
Copy link
Contributor

bors commented May 2, 2020

📌 Commit 461f4a3 has been approved by phansch

@bors
Copy link
Contributor

bors commented May 2, 2020

⌛ Testing commit 461f4a3 with merge e3b8c41...

@bors
Copy link
Contributor

bors commented May 2, 2020

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

@bors bors merged commit e3b8c41 into rust-lang:master May 2, 2020
@rail-rain rail-rain deleted the fix_manual_memcpy branch May 5, 2020 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants