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

Fixed FP for thread_local_initializer_can_be_made_const for os_local #12186

Closed
wants to merge 33 commits into from

Conversation

m-rph
Copy link
Contributor

@m-rph m-rph commented Jan 22, 2024

os_local impl of thread_local — regardless of whether it is const and unlike other implementations — includes an fn __init() -> EXPR.

Existing implementation of the lint checked for the presence of said function and whether the expr can be made const. Because for os_local we always have an __init(), it triggers for const qualified and const-qualifiable.

The solution is to check whether the __init() function is already const. If it is const, there is nothing to do. Otherwise, we can make it const.

changelog: [THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST]: fixed FP for os_local implementation of thread_local_inner!.

@rustbot
Copy link
Collaborator

rustbot commented Jan 22, 2024

r? @xFrednet

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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 22, 2024
@m-rph
Copy link
Contributor Author

m-rph commented Jan 22, 2024

r? @llogiq

@rustbot
Copy link
Collaborator

rustbot commented Jan 22, 2024

Failed to set assignee to petrochenkov: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@rustbot rustbot assigned llogiq and unassigned xFrednet Jan 22, 2024
@m-rph
Copy link
Contributor Author

m-rph commented Jan 22, 2024

I am not sure how to verify this. @petrochenkov since you reported it, if you don't mind, could you verify?

@llogiq
Copy link
Contributor

llogiq commented Jan 27, 2024

I'd agree that this should have some kind of test, or alternatively an explanation why it cannot be tested.

@m-rph
Copy link
Contributor Author

m-rph commented Jan 27, 2024

@llogiq does the clippy test-suite include a cygwin backend?

A potential issue is that we might experience different things for specific platforms which makes clippy development harder.

@llogiq
Copy link
Contributor

llogiq commented Jan 28, 2024

No, we currently don't have cygwin. I'd like to understand the difference that makes to the lint, though.

@m-rph
Copy link
Contributor Author

m-rph commented Jan 28, 2024

The difference relates to which implementation is used. Threadlocal has 3 implementations whivh get conditionally compiled based on the platform.

For windows, threadlocal uses os native threads, for linux it uses llvm and thread_local_fast. Finally for wasm there is a "static" implementation.

For the former, we always have an init function regardless of whether the expression is const. If threadlocal is const, the function is a const fn.

For thread local fast, if const is used, the value is used directly by threadlocal, and we don't have the init fn. if there is no const, an init fn is used.

For static, there is no init fn either.

So with the lint we check if we are in init fn.
We then check if we can make the function const and with the changes if the function isn't already const.

Does this make sense?

@mati865
Copy link
Contributor

mati865 commented Jan 28, 2024

Wouldn't Cygwin (once or maybe if it's added) use Windows implementation since it runs on Windows? Or does it provide some wrappers to emulate Unix thread-local?

@m-rph
Copy link
Contributor Author

m-rph commented Jan 28, 2024

Cygwin will use the os native implementation once added, yes.

@llogiq
Copy link
Contributor

llogiq commented Jan 29, 2024

We have a few windows-specific tests already (I believe we used to put them in a "ui.windows" test subfolder or some such. Could that work in this case?

@m-rph
Copy link
Contributor Author

m-rph commented Feb 3, 2024

@llogiq I don't think the tests are actually running in the build scripts. Clippy test workflow only has ubuntu as a target.

@llogiq
Copy link
Contributor

llogiq commented Feb 3, 2024

That's ok, I have a Windows partition I can run those tests on locally.

@m-rph
Copy link
Contributor Author

m-rph commented Feb 3, 2024

Ok, so in theory if you run the code as-is, right now, it should fail because of differences between .stderr files.

@m-rph m-rph marked this pull request as draft February 9, 2024 21:12
adamreichold and others added 9 commits February 11, 2024 13:47
Checks for the suspicious use of OpenOptions::create()
without an explicit OpenOptions::truncate().

create() alone will either create a new file or open an
existing file. If the file already exists, it will be
overwritten when written to, but the file will not be
truncated by default. If less data is written to the file
than it already contains, the remainder of the file will
remain unchanged, and the end of the file will contain old
data.
In most cases, one should either use `create_new` to ensure
the file is created from scratch, or ensure `truncate` is
called so that the truncation behaviour is explicit.
`truncate(true)` will ensure the file is entirely overwritten
with new data, whereas `truncate(false)` will explicitely
keep the default behavior.

```rust
use std::fs::OpenOptions;

OpenOptions::new().create(true).truncate(true);
```
Also rebase and fix conflicts
- New ineffective_open_options had to be fixed.
- Now not raising an issue on missing `truncate` when `append(true)`
  makes the intent clear.
- Try implementing more advanced tests for non-chained operations. Fail
GuillaumeGomez and others added 22 commits February 11, 2024 13:47
Prefixing a variable with a `_` does not mean that it will not be used.
If such a variable is used later, do not warn about the fact that its
initialization does not have a side effect as this is fine.
Partial rewrite of `unused_io_account` to lint over Ok(_).

Moved the check to `check_block` to simplify context checking for
expressions and allow us to check only some expressions.

For match (expr, arms) we emit a lint for io ops used on `expr` when an
arm is `Ok(_)`. Also considers the cases when there are guards in the
arms. It also captures `if let Ok(_) = ...` cases.

For `Ok(_)` it emits a note indicating where the value is ignored.

changelog: False Negatives [`unused_io_amount`]: Extended
`unused_io_amount` to catch `Ok(_)`s in `If let` and match exprs.
`os_local` impl of `thread_local` — regardless of whether it is const and
unlike other implementations — includes an `fn __init() -> EXPR`.

Existing implementation of the lint checked for the presence of said
function and whether the expr can be made const. Because for `os_local`
we always have an `__init()`, it triggers for const implementations.

The solution is to check whether the `__init()` function is already const.
If it is `const`, there is nothing to do. Otherwise, we verify that we can
make it const.
@m-rph m-rph closed this Feb 11, 2024
@m-rph m-rph deleted the hide-thread-local branch February 11, 2024 13:11
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.

None yet