Skip to content

Commit 735f758

Browse files
committed
Auto merge of rust-lang#122149 - flip1995:clippy-subtree-update, r=Manishearth
Clippy subtree update r? `@Manishearth`
2 parents 1c580bc + 73f7e79 commit 735f758

File tree

155 files changed

+4143
-2430
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+4143
-2430
lines changed

Diff for: src/tools/clippy/.github/driver.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ diff -u normalized.stderr tests/ui/double_neg.stderr
5050

5151
# make sure "clippy-driver --rustc --arg" and "rustc --arg" behave the same
5252
SYSROOT=$(rustc --print sysroot)
53-
diff -u <(LD_LIBRARY_PATH=${SYSROOT}/lib ./target/debug/clippy-driver --rustc --version --verbose) <(rustc --version --verbose)
53+
diff -u <(./target/debug/clippy-driver --rustc --version --verbose) <(rustc --version --verbose)
5454

5555
echo "fn main() {}" >target/driver_test.rs
5656
# we can't run 2 rustcs on the same file at the same time
57-
CLIPPY=$(LD_LIBRARY_PATH=${SYSROOT}/lib ./target/debug/clippy-driver ./target/driver_test.rs --rustc)
57+
CLIPPY=$(./target/debug/clippy-driver ./target/driver_test.rs --rustc)
5858
RUSTC=$(rustc ./target/driver_test.rs)
5959
diff -u <($CLIPPY) <($RUSTC)
6060

Diff for: src/tools/clippy/.github/workflows/clippy.yml

+3-8
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ jobs:
4444
run: rustup show active-toolchain
4545

4646
# Run
47-
- name: Set LD_LIBRARY_PATH (Linux)
48-
run: |
49-
SYSROOT=$(rustc --print sysroot)
50-
echo "LD_LIBRARY_PATH=${SYSROOT}/lib${LD_LIBRARY_PATH+:${LD_LIBRARY_PATH}}" >> $GITHUB_ENV
51-
5247
- name: Build
5348
run: cargo build --tests --features deny-warnings,internal
5449

@@ -72,6 +67,6 @@ jobs:
7267
working-directory: clippy_dev
7368

7469
- name: Test clippy-driver
75-
run: bash .github/driver.sh
76-
env:
77-
OS: ${{ runner.os }}
70+
run: |
71+
TOOLCHAIN=$(rustup show active-toolchain | cut -f1 -d' ')
72+
rustup run $TOOLCHAIN bash .github/driver.sh

Diff for: src/tools/clippy/.github/workflows/clippy_bors.yml

+6-26
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
host: i686-unknown-linux-gnu
6060
- os: windows-latest
6161
host: x86_64-pc-windows-msvc
62-
- os: macos-latest
62+
- os: macos-13
6363
host: x86_64-apple-darwin
6464

6565
runs-on: ${{ matrix.os }}
@@ -87,23 +87,6 @@ jobs:
8787
rustup show active-toolchain
8888
8989
# Run
90-
- name: Set LD_LIBRARY_PATH (Linux)
91-
if: runner.os == 'Linux'
92-
run: |
93-
SYSROOT=$(rustc --print sysroot)
94-
echo "LD_LIBRARY_PATH=${SYSROOT}/lib${LD_LIBRARY_PATH+:${LD_LIBRARY_PATH}}" >> $GITHUB_ENV
95-
- name: Link rustc dylib (MacOS)
96-
if: runner.os == 'macOS'
97-
run: |
98-
SYSROOT=$(rustc --print sysroot)
99-
sudo mkdir -p /usr/local/lib
100-
sudo find "${SYSROOT}/lib" -maxdepth 1 -name '*dylib' -exec ln -s {} /usr/local/lib \;
101-
- name: Set PATH (Windows)
102-
if: runner.os == 'Windows'
103-
run: |
104-
SYSROOT=$(rustc --print sysroot)
105-
echo "$SYSROOT/bin" >> $GITHUB_PATH
106-
10790
- name: Build
10891
run: cargo build --tests --features deny-warnings,internal
10992

@@ -136,7 +119,9 @@ jobs:
136119
working-directory: clippy_dev
137120

138121
- name: Test clippy-driver
139-
run: bash .github/driver.sh
122+
run: |
123+
TOOLCHAIN=$(rustup show active-toolchain | cut -f1 -d' ')
124+
rustup run $TOOLCHAIN bash .github/driver.sh
140125
env:
141126
OS: ${{ runner.os }}
142127

@@ -236,11 +221,6 @@ jobs:
236221
- name: Install toolchain
237222
run: rustup show active-toolchain
238223

239-
- name: Set LD_LIBRARY_PATH
240-
run: |
241-
SYSROOT=$(rustc --print sysroot)
242-
echo "LD_LIBRARY_PATH=${SYSROOT}/lib${LD_LIBRARY_PATH+:${LD_LIBRARY_PATH}}" >> $GITHUB_ENV
243-
244224
# Download
245225
- name: Download target dir
246226
uses: actions/download-artifact@v3
@@ -254,8 +234,8 @@ jobs:
254234
# Run
255235
- name: Test ${{ matrix.integration }}
256236
run: |
257-
RUSTUP_TOOLCHAIN="$(rustup show active-toolchain | grep -o -E "nightly-[0-9]{4}-[0-9]{2}-[0-9]{2}")" \
258-
$CARGO_TARGET_DIR/debug/integration --show-output
237+
TOOLCHAIN=$(rustup show active-toolchain | cut -f1 -d' ')
238+
rustup run $TOOLCHAIN $CARGO_TARGET_DIR/debug/integration --show-output
259239
env:
260240
INTEGRATION: ${{ matrix.integration }}
261241

Diff for: src/tools/clippy/CHANGELOG.md

+57-55
Large diffs are not rendered by default.

Diff for: src/tools/clippy/book/src/development/trait_checking.md

+50
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,53 @@ impl LateLintPass<'_> for CheckTokioAsyncReadExtTrait {
9494
}
9595
```
9696

97+
## Creating Types Programmatically
98+
99+
Traits are often generic over a type parameter, e.g. `Borrow<T>` is generic
100+
over `T`. Rust allows us to implement a trait for a specific type. For example,
101+
we can implement `Borrow<[u8]>` for a hypothetical type `Foo`. Let's suppose
102+
that we would like to find whether our type actually implements `Borrow<[u8]>`.
103+
104+
To do so, we can use the same `implements_trait` function as above, and supply
105+
a type parameter that represents `[u8]`. Since `[u8]` is a specialization of
106+
`[T]`, we can use the [`Ty::new_slice`][new_slice] method to create a type
107+
that represents `[T]` and supply `u8` as a type parameter.
108+
To create a `ty::Ty` programmatically, we rely on `Ty::new_*` methods. These
109+
methods create a `TyKind` and then wrap it in a `Ty` struct. This means we
110+
have access to all the primitive types, such as `Ty::new_char`,
111+
`Ty::new_bool`, `Ty::new_int`, etc. We can also create more complex types,
112+
such as slices, tuples, and references out of these basic building blocks.
113+
114+
For trait checking, it is not enough to create the types, we need to convert
115+
them into [GenericArg]. In rustc, a generic is an entity that the compiler
116+
understands and has three kinds, type, const and lifetime. By calling
117+
`.into()` on a constructed [Ty], we wrap the type into a generic which can
118+
then be used by the query system to decide whether the specialized trait
119+
is implemented.
120+
121+
The following code demonstrates how to do this:
122+
123+
```rust
124+
125+
use rustc_middle::ty::Ty;
126+
use clippy_utils::ty::implements_trait;
127+
use rustc_span::symbol::sym;
128+
129+
let ty = todo!("Get the `Foo` type to check for a trait implementation");
130+
let borrow_id = cx.tcx.get_diagnostic_item(sym::Borrow).unwrap(); // avoid unwrap in real code
131+
let slice_of_bytes_t = Ty::new_slice(cx.tcx, cx.tcx.types.u8);
132+
let generic_param = slice_of_bytes_t.into();
133+
if implements_trait(cx, ty, borrow_id, &[generic_param]) {
134+
todo!("Rest of lint implementation")
135+
}
136+
```
137+
138+
In essence, the [Ty] struct allows us to create types programmatically in a
139+
representation that can be used by the compiler and the query engine. We then
140+
use the `rustc_middle::Ty` of the type we are interested in, and query the
141+
compiler to see if it indeed implements the trait we are interested in.
142+
143+
97144
[DefId]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefId.html
98145
[diagnostic_items]: https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-items.html
99146
[lang_items]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/lang_items/struct.LanguageItems.html
@@ -102,4 +149,7 @@ impl LateLintPass<'_> for CheckTokioAsyncReadExtTrait {
102149
[symbol]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/symbol/struct.Symbol.html
103150
[symbol_index]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/sym/index.html
104151
[TyCtxt]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html
152+
[Ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html
105153
[rust]: https://github.com/rust-lang/rust
154+
[new_slice]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html#method.new_slice
155+
[GenericArg]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.GenericArg.html

Diff for: src/tools/clippy/book/src/development/type_checking.md

+16
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ the [`TypeckResults::node_type()`][node_type] method inside of bodies.
123123

124124
> **Warning**: Don't use `hir_ty_to_ty` inside of bodies, because this can cause ICEs.
125125
126+
## Creating Types programmatically
127+
128+
A common usecase for creating types programmatically is when we want to check if a type implements a trait (see
129+
[Trait Checking](trait_checking.md)).
130+
131+
Here's an example of how to create a `Ty` for a slice of `u8`, i.e. `[u8]`
132+
133+
```rust
134+
use rustc_middle::ty::Ty;
135+
// assume we have access to a LateContext
136+
let ty = Ty::new_slice(cx.tcx, Ty::new_u8());
137+
```
138+
139+
In general, we rely on `Ty::new_*` methods. These methods define the basic building-blocks that the
140+
type-system and trait-system use to define and understand the written code.
141+
126142
## Useful Links
127143

128144
Below are some useful links to further explore the concepts covered

0 commit comments

Comments
 (0)