Commit graph

232 commits

Author SHA1 Message Date
Obei Sideg
01cfa9aad5
Add hard error for extern without explicit ABI 2025-04-16 22:43:56 +03:00
bors
7bfd9529be Auto merge of #119220 - Urgau:uplift-invalid_null_ptr_usage, r=fee1-dead
Uplift `clippy::invalid_null_ptr_usage` lint as `invalid_null_arguments`

This PR aims at uplifting the `clippy::invalid_null_ptr_usage` lint into rustc, this is similar to the [`clippy::invalid_utf8_in_unchecked` uplift](https://github.com/rust-lang/rust/pull/111543) a few months ago, in the sense that those two lints lint on invalid parameter(s), here a null pointer where it is unexpected and UB to pass one.

*For context: GitHub Search reveals that just for `slice::from_raw_parts{_mut}` [~20 invalid usages](hhttps://github.com/search?q=lang%3Arust+%2Fslice%3A%3Afrom_raw_parts%28_mut%29%3F%5C%28ptr%3A%3Anull%2F+NOT+path%3A%2F%5Eclippy_lints%5C%2Fsrc%5C%2F%2F+NOT+path%3A%2F%5Erust%5C%2Fsrc%5C%2Ftools%5C%2Fclippy%5C%2Fclippy_lints%5C%2Fsrc%5C%2F%2F+NOT+path%3A%2F%5Esrc%5C%2Ftools%5C%2Fclippy%5C%2Fclippy_lints%5C%2Fsrc%5C%2F%2F&type=code) with `ptr::null` and an additional [4 invalid usages](https://github.com/search?q=lang%3Arust+%2Fslice%3A%3Afrom_raw_parts%5C%280%28%5C%29%7C+as%29%2F+NOT+path%3A%2F%5Eclippy_lints%5C%2Fsrc%5C%2F%2F+NOT+path%3A%2F%5Erust%5C%2Fsrc%5C%2Ftools%5C%2Fclippy%5C%2Fclippy_lints%5C%2Fsrc%5C%2F%2F+NOT+path%3A%2F%5Esrc%5C%2Ftools%5C%2Fclippy%5C%2Fclippy_lints%5C%2Fsrc%5C%2F%2F+NOT+path%3A%2F%5Eutils%5C%2Ftinystr%5C%2Fsrc%5C%2F%2F+NOT+path%3A%2F%5Eutils%5C%2Fzerovec%5C%2Fsrc%5C%2F%2F+NOT+path%3A%2F%5Eprovider%5C%2Fcore%5C%2Fsrc%5C%2F%2F&type=code) with `0 as *const ...`-ish casts.*

-----

## `invalid_null_arguments`

(deny-by-default)

The `invalid_null_arguments` lint checks for invalid usage of null pointers.

### Example

```rust
// Undefined behavior
unsafe { std::slice::from_raw_parts(ptr::null(), 1); }
```

Produces:
```
error: calling this function with a null pointer is Undefined Behavior, even if the result of the function is unused
  --> $DIR/invalid_null_args.rs:21:23
   |
LL |     let _: &[usize] = std::slice::from_raw_parts(ptr::null_mut(), 0);
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^
   |                                                  |
   |                                                  null pointer originates from here
   |
   = help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
```

### Explanation

Calling methods whose safety invariants requires non-null pointer with a null pointer is undefined behavior.

-----

The lint use a list of functions to know which functions and arguments to checks, this could be improved in the future with a rustc attribute, or maybe even with a `#[diagnostic]` attribute.

This PR also includes some small refactoring to avoid some ambiguities in naming, those can be done in another PR is desired.

`@rustbot` label: +I-lang-nominated
r? compiler
2025-03-31 04:17:14 +00:00
Urgau
96a2f69844 Uplift clippy::invalid_null_ptr_usage as invalid_null_arguments 2025-03-30 19:33:15 +02:00
Urgau
a20d2ef0d9 Improve explicitness of the impl of the useless_ptr_null_checks lint 2025-03-30 12:14:02 +02:00
Michael Goulet
14804d1ed1 Implement lint against using Interner and InferCtxtLike in random compiler crates 2025-03-26 04:39:38 +00:00
Michael Goulet
864cca80b0 Print out destructor 2025-02-26 19:03:29 +00:00
Esteban Küber
835d434c79 Reword message 2025-02-20 17:55:31 +00:00
Esteban Küber
fe7ed278b7 Specify scope in out_of_scope_macro_calls lint
```
warning: cannot find macro `in_root` in the crate root
  --> $DIR/key-value-expansion-scope.rs:1:10
   |
LL | #![doc = in_root!()]
   |          ^^^^^^^ not found in the crate root
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #124535 <https://github.com/rust-lang/rust/issues/124535>
   = help: import `macro_rules` with `use` to make it callable above its definition
   = note: `#[warn(out_of_scope_macro_calls)]` on by default
```
2025-02-19 18:29:00 +00:00
Pavel Grigorenko
f53d0f502d invalid_from_utf8[_unchecked]: also lint inherent methods 2025-02-16 16:34:51 +03:00
Oli Scherer
473352da31 Correctly handle pattern types in FFI safety 2025-02-11 08:30:35 +00:00
Kalle Wachsmuth
c1dcbebd0b
implement lint double_negations 2025-01-26 12:18:33 +01:00
Jacob Pratt
dc202df0ed
Rollup merge of #133951 - bjorn3:wasm_c_abi_lint_hard_error, r=workingjubilee
Make the wasm_c_abi future compat warning a hard error

This is the next step in getting rid of the broken C abi for wasm32-unknown-unknown.

The lint was made deny-by-default in https://github.com/rust-lang/rust/pull/129534 3 months ago. This still keeps the `-Zwasm-c-abi` flag set to `legacy` by default. It will be flipped in a future PR.

cc https://github.com/rust-lang/rust/issues/122532
2025-01-25 23:26:58 -05:00
Matthias Krüger
f01d418139
Rollup merge of #134300 - RalfJung:remove-dead-attrs, r=chenyukang
remove long-deprecated no-op attributes no_start and crate_id

These have emitted a deprecation warning since forever (https://github.com/rust-lang/rust/pull/64471) and they already don't do anything. In fact they [apparently](https://github.com/rust-lang/rust/pull/64471#issuecomment-531517332) have done nothing since pre-1.0, so... do we even need a crater run? Doesn't seem worth it.
2025-01-25 23:15:22 +01:00
bjorn3
4d1c16c09c Make the wasm_c_abi future compat warning a hard error
This is the next step in getting rid of the broken C abi for
wasm32-unknown-unknown.
2025-01-23 10:22:23 +00:00
Matthias Krüger
9b40bd70de
Rollup merge of #135552 - amy-kwan:amy-kwan/reprc-struct-diagnostic-power-alignment, r=workingjubilee
[AIX] Lint on structs that have a different alignment in AIX's C ABI

This PR adds a linting diagnostic on AIX for repr(C) structs that are required to follow
the power alignment rule. A repr(C) struct needs to follow the power alignment rule if
the struct:
- Has a floating-point data type (greater than 4-bytes) as its first member, or
- The first member of the struct is an aggregate, whose recursively first member is a
   floating-point data type (greater than 4-bytes).

The power alignment rule for eligible structs is currently unimplemented, so a linting
diagnostic is produced when such a struct is encountered.
2025-01-23 09:49:19 +01:00
Matthias Krüger
9206ba535c
Rollup merge of #132983 - Anthony-Eid:dangling-pointers-lint, r=Urgau
Edit dangling pointers

Closes: #132283
2025-01-22 20:37:23 +01:00
Amy Kwan
cd2ecc4b50 [AIX] Lint on structs that have a different alignment in AIX's C ABI 2025-01-22 12:06:16 -05:00
Anthony Eid
12214db74b Update lint tests with new dangling pointers message 2025-01-22 00:00:31 -05:00
Ralf Jung
a99778c839 remove long-deprecated no-op attributes no_start and crate_id 2025-01-21 17:29:06 -07:00
Jacob Pratt
52b4557639
Rollup merge of #134202 - nnethercote:rm-existing_doc_keyword, r=GuillaumeGomez
Remove `rustc::existing_doc_keyword` lint

The check doesn't require a lint.

r? ``@GuillaumeGomez``
2024-12-17 05:36:52 -05:00
Nicholas Nethercote
121e87bf14 Remove rustc::existing_doc_keyword lint.
`CheckAttrVisitor::check_doc_keyword` checks `#[doc(keyword = "..")]`
attributes to ensure they are on an empty module, and that the value is
a non-empty identifier.

The `rustc::existing_doc_keyword` lint checks these attributes to ensure
that the value is the name of a keyword.

It's silly to have two different checking mechanisms for these
attributes. This commit does the following.
- Changes `check_doc_keyword` to check that the value is the name of a
  keyword (avoiding the need for the identifier check, which removes a
  dependency on `rustc_lexer`).
- Removes the lint.
- Updates tests accordingly.

There is one hack: the `SelfTy` FIXME case used to used to be handled by
disabling the lint, but now is handled with a special case in
`is_doc_keyword`. That hack will go away if/when the FIXME is fixed.

Co-Authored-By: Guillaume Gomez <guillaume1.gomez@gmail.com>
2024-12-17 13:56:10 +11:00
Anton Lazarev
e9df8216d5
spell "referring" correctly 2024-12-15 19:36:35 -08:00
Urgau
291c519c69 Improve check-cfg Cargo macro diagnostic with crate name 2024-12-15 17:16:01 +01:00
Matthias Krüger
f96fdab101
Rollup merge of #133221 - Urgau:check-cfg-macro-diag, r=jieyouxu
Add external macros specific diagnostics for check-cfg

This PR adds specific check-cfg diagnostics for unexpected cfg in external macros.

As well as hiding the some of the Cargo specific help/suggestions as they distraction for external macros and are generally not the right solution.

Follow-up to #132577

`@rustbot` label +L-unexpected_cfgs
r? compiler
2024-12-14 14:07:56 +01:00
许杰友 Jieyou Xu (Joe)
1fcbb1e338 Revert #131669 due to ICEs
Revert <https://github.com/rust-lang/rust/pull/131669> due to ICE
reports:

- <https://github.com/rust-lang/rust/issues/134059> (real-world)
- <https://github.com/rust-lang/rust/issues/134060> (fuzzing)

The changes can be re-landed with those cases addressed.

This reverts commit 703bb98230, reversing
changes made to f415c07494.
2024-12-09 17:31:16 +08:00
niacdoial
8b6289f6ae lint ImproperCTypes: message tweaks and refactoring from code review 2024-12-06 22:23:13 +01:00
niacdoial
1d52131043 lint: rework some ImproperCTypes messages (especially around indirections to !Sized) 2024-12-06 22:23:13 +01:00
Anthony Eid
bab8a41334 Update compiler/rustc_lint/messages.ftl
Co-authored-by: Urgau <3616612+Urgau@users.noreply.github.com>
2024-12-05 06:12:27 -05:00
Anthony Eid
de0cb6cb6e Start work on dangling pointers lint 2024-12-05 06:12:27 -05:00
León Orell Valerian Liehr
35ea48d588
Rollup merge of #118833 - Urgau:lint_function_pointer_comparisons, r=cjgillot
Add lint against function pointer comparisons

This is kind of a follow-up to https://github.com/rust-lang/rust/pull/117758 where we added a lint against wide pointer comparisons for being ambiguous and unreliable; well function pointer comparisons are also unreliable. We should IMO follow a similar logic and warn people about it.

-----

## `unpredictable_function_pointer_comparisons`

*warn-by-default*

The `unpredictable_function_pointer_comparisons` lint checks comparison of function pointer as the operands.

### Example

```rust
fn foo() {}
let a = foo as fn();

let _ = a == foo;
```

### Explanation

Function pointers comparisons do not produce meaningful result since they are never guaranteed to be unique and could vary between different code generation units. Furthermore different function could have the same address after being merged together.

----

This PR also uplift the very similar `clippy::fn_address_comparisons` lint, which only linted on if one of the operand was an `ty::FnDef` while this PR lints proposes to lint on all `ty::FnPtr` and `ty::FnDef`.

```@rustbot``` labels +I-lang-nominated

~~Edit: Blocked on https://github.com/rust-lang/libs-team/issues/323 being accepted and it's follow-up pr~~
2024-12-05 07:29:53 +01:00
Matthias Krüger
453a1a8b7f
Rollup merge of #133545 - clubby789:symbol-intern-lit, r=jieyouxu
Lint against Symbol::intern on a string literal

Disabled in tests where this doesn't make much sense
2024-12-03 17:27:06 +01:00
Urgau
9d1f790594 Add warn-by-default lint against unpredictable fn pointer comparisons 2024-12-02 18:43:37 +01:00
HomelikeBrick42
4cb158278c Fixed typos by changing happend to happened 2024-12-01 11:31:09 +13:00
clubby789
72d2db7bf4 Implement lint against Symbol::intern on a string literal 2024-11-28 15:45:25 +00:00
Peter Jaszkowiak
44f4f67f46 fix confusing diagnostic for reserved ## 2024-11-25 22:29:14 -07:00
Ding Xiang Fei
297b618944
reduce false positives of tail-expr-drop-order from consumed values
take 2

open up coroutines

tweak the wordings

the lint works up until 2021

We were missing one case, for ADTs, which was
causing `Result` to yield incorrect results.

only include field spans with significant types

deduplicate and eliminate field spans

switch to emit spans to impl Drops

Co-authored-by: Niko Matsakis <nikomat@amazon.com>

collect drops instead of taking liveness diff

apply some suggestions and add explantory notes

small fix on the cache

let the query recurse through coroutine

new suggestion format with extracted variable name

fine-tune the drop span and messages

bugfix on runtime borrows

tweak message wording

filter out ecosystem types earlier

apply suggestions

clippy

check lint level at session level

further restrict applicability of the lint

translate bid into nop for stable mir

detect cycle in type structure
2024-11-20 20:53:11 +08:00
Urgau
e2fbeec150 Add external macro specific diagnostic to check-cfg 2024-11-19 23:09:58 +01:00
Michael Goulet
ad20906065 Suggest turning APITs into generics in opaque overcaptures 2024-11-09 19:18:22 +00:00
Mara Bos
cb26fa07bb Improve the missing_abi lint. 2024-10-31 10:55:45 +01:00
Pavel Grigorenko
c69894eaec New lint: dangling_pointers_from_temporaries 2024-10-28 14:16:05 +03:00
Urgau
77b3065ed2 Remove deprecation note in the non_local_definitions warning 2024-10-11 21:21:32 +02:00
Peter Jaszkowiak
321a5db7d4 Reserve guarded string literals (RFC 3593) 2024-10-08 18:21:16 -06:00
bors
8422e27b27 Auto merge of #129670 - est31:cfg_attr_crate_type_name_error, r=Urgau
Make deprecated_cfg_attr_crate_type_name a hard error

Turns the forward compatibility lint added by #83744 into a hard error, so now, while the `#![crate_name]` and `#![crate_type]` attributes are still allowed in raw form, they are now forbidden to be nested inside a `#![cfg_attr()]` attribute.

The following will now be an error:

```Rust
#![cfg_attr(foo, crate_name = "foobar")]
#![cfg_attr(foo, crate_type = "bin")]
```

This code will continue working and is not deprecated:

```Rust
#![crate_name = "foobar"]
#![crate_type = "lib"]
```

The reasoning for this is explained in #83744: it allows us to not have to cfg-expand in order to determine the crate's type and name.

As of filing the PR, exactly two years have passed since #99784 has been merged, which has turned the lint's default warning level into an error, so there has been ample time to move off the now-forbidden syntax.

cc #91632 - tracking issue for the lint
2024-10-06 17:00:02 +00:00
est31
00ed47b849 Make deprecated_cfg_attr_crate_type_name a hard error 2024-10-05 04:29:46 +02:00
Matthias Krüger
a935064fae
Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, r=compiler-errors
Compiler: Rename "object safe" to "dyn compatible"

Completed T-lang FCP: https://github.com/rust-lang/lang-team/issues/286#issuecomment-2338905118.
Tracking issue: https://github.com/rust-lang/rust/issues/130852

Excludes `compiler/rustc_codegen_cranelift` (to be filed separately).
Includes Stable MIR.

Regarding https://github.com/rust-lang/rust/labels/relnotes, I guess I will manually open a https://github.com/rust-lang/rust/labels/relnotes-tracking-issue since this change affects everything (compiler, library, tools, docs, books, everyday language).

r? ghost
2024-09-27 21:35:08 +02:00
makai410
58921874cb Fix the misleading diagnostic for let_underscore_drop on type without Drop implementation 2024-09-26 10:18:18 +08:00
León Orell Valerian Liehr
01a063f9df
Compiler: Rename "object safe" to "dyn compatible" 2024-09-25 13:26:48 +02:00
bors
4cadeda932 Auto merge of #130768 - compiler-errors:rollup-8ncjy55, r=compiler-errors
Rollup of 7 pull requests

Successful merges:

 - #129545 (rustdoc: redesign toolbar and disclosure widgets)
 - #130618 (Skip query in get_parent_item when possible.)
 - #130727 (Check vtable projections for validity in miri)
 - #130750 (Add new Tier-3 target: `loongarch64-unknown-linux-ohos`)
 - #130758 (Revert "Add recursion limit to FFI safety lint")
 - #130759 (Update books)
 - #130762 (stabilize const_intrinsic_copy)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-09-24 06:02:43 +00:00
Michael Goulet
fa1bd9b06a
Rollup merge of #130758 - compiler-errors:ctype-recursion-limit, r=jieyouxu
Revert "Add recursion limit to FFI safety lint"

It's not necessarily clear if warning when we hit the recursion limit is the right thing to do, first of all.

**More importantly**, this PR was implemented incorrectly in the first place; it was not decrementing the recursion limit when stepping out of a type, so it would trigger when a ctype has more than RECURSION_LIMIT fields *anywhere* in the type's set of recursively reachable fields.

Reverts #130598
Reopens #130310
Fixes #130757
2024-09-23 23:49:13 -04:00
bors
f5cd2c5888 Auto merge of #127117 - Urgau:non_local_def-syntactic, r=BoxyUwU
Rework `non_local_definitions` lint to only use a syntactic heuristic

This PR reworks the `non_local_definitions` lint to only use a syntactic heuristic, i.e. not use a type-system logic for whenever an `impl` is local or not.

Instead the new logic wanted by T-lang in https://github.com/rust-lang/rust/issues/126768#issuecomment-2192634762, which is to consider every paths in `Self` and `Trait` and to no longer use the type-system inference trick.

`@rustbot` labels +L-non_local_definitions
Fixes #126768
2024-09-24 03:43:01 +00:00