Skip to content

Commit

Permalink
Run CI checks for Rust 2024 as well
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Nov 3, 2024
3 parents 52b8324 + 540e116 + 8052451 commit 792f6ab
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 24 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,52 @@ jobs:
run: .github/driver.sh
env:
OS: ${{ runner.os }}

base-2024:
# NOTE: If you modify this job, make sure you copy the changes to clippy_bors.yml
runs-on: ubuntu-latest

steps:
# Setup
- name: Checkout
uses: actions/checkout@v4

- name: Select Rust 2024 edition
run: |
for i in clippy_config clippy_dev clippy_lints clippy_utils lintcheck; do
sed -i \
-e '1icargo-features = ["edition2024"]' \
-e 's/edition = "2021"/edition = "2024"/' \
$i/Cargo.toml
done
- name: Install toolchain
run: rustup show active-toolchain

# Run
- name: Build
run: cargo build --tests --features internal

- name: Test
run: cargo test --features internal

- name: Test clippy_lints
run: cargo test --features internal
working-directory: clippy_lints

- name: Test clippy_utils
run: cargo test
working-directory: clippy_utils

- name: Test rustc_tools_util
run: cargo test
working-directory: rustc_tools_util

- name: Test clippy_dev
run: cargo test
working-directory: clippy_dev

- name: Test clippy-driver
run: .github/driver.sh
env:
OS: ${{ runner.os }}
17 changes: 17 additions & 0 deletions .github/workflows/clippy_bors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ jobs:
include:
- os: ubuntu-latest
host: x86_64-unknown-linux-gnu
edition: 2021
- os: ubuntu-latest
host: x86_64-unknown-linux-gnu
edition: 2024
- os: ubuntu-latest
host: i686-unknown-linux-gnu
edition: 2021
- os: windows-latest
host: x86_64-pc-windows-msvc
edition: 2021
- os: macos-13
host: x86_64-apple-darwin
edition: 2021

runs-on: ${{ matrix.os }}

Expand All @@ -79,6 +86,16 @@ jobs:
sudo apt-get update
sudo apt-get install gcc-multilib zlib1g-dev:i386
- name: Select Rust 2024 edition
if: matrix.edition == 2024
run: |
for i in clippy_config clippy_dev clippy_lints clippy_utils lintcheck; do
sed -i \
-e '1icargo-features = ["edition2024"]' \
-e 's/edition = "2021"/edition = "2024"/' \
$i/Cargo.toml
done
- name: Install toolchain
run: |
rustup set default-host ${{ matrix.host }}
Expand Down
13 changes: 7 additions & 6 deletions clippy_lints/src/no_mangle_with_rust_abi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::source::{snippet, snippet_with_applicability};
use rustc_errors::Applicability;
use rustc_hir::{Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
Expand All @@ -18,13 +18,13 @@ declare_clippy_lint! {
/// Rust ABI can break this at any point.
///
/// ### Example
/// ```no_run
/// ```rust,ignore
/// #[no_mangle]
/// fn example(arg_one: u32, arg_two: usize) {}
/// ```
///
/// Use instead:
/// ```no_run
/// ```rust,ignore
/// #[no_mangle]
/// extern "C" fn example(arg_one: u32, arg_two: usize) {}
/// ```
Expand All @@ -40,24 +40,25 @@ impl<'tcx> LateLintPass<'tcx> for NoMangleWithRustAbi {
if let ItemKind::Fn(fn_sig, _, _) = &item.kind {
let attrs = cx.tcx.hir().attrs(item.hir_id());
let mut app = Applicability::MaybeIncorrect;
let snippet = snippet_with_applicability(cx, fn_sig.span, "..", &mut app);
let fn_snippet = snippet_with_applicability(cx, fn_sig.span, "..", &mut app);
for attr in attrs {
if let Some(ident) = attr.ident()
&& ident.name == rustc_span::sym::no_mangle
&& fn_sig.header.abi == Abi::Rust
&& let Some((fn_attrs, _)) = snippet.split_once("fn")
&& let Some((fn_attrs, _)) = fn_snippet.split_once("fn")
&& !fn_attrs.contains("extern")
{
let sugg_span = fn_sig
.span
.with_lo(fn_sig.span.lo() + BytePos::from_usize(fn_attrs.len()))
.shrink_to_lo();
let attr_snippet = snippet(cx, attr.span, "..");

span_lint_and_then(
cx,
NO_MANGLE_WITH_RUST_ABI,
fn_sig.span,
"`#[no_mangle]` set on a function with the default (`Rust`) ABI",
format!("`{attr_snippet}` set on a function with the default (`Rust`) ABI"),
|diag| {
diag.span_suggestion(sugg_span, "set an ABI", "extern \"C\" ", app)
.span_suggestion(sugg_span, "or explicitly set the default", "extern \"Rust\" ", app);
Expand Down
2 changes: 1 addition & 1 deletion clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ pub fn def_path_res_with_base(tcx: TyCtxt<'_>, mut base: Vec<Res>, mut path: &[&
}

/// Resolves a def path like `std::vec::Vec` to its [`DefId`]s, see [`def_path_res`].
pub fn def_path_def_ids(tcx: TyCtxt<'_>, path: &[&str]) -> impl Iterator<Item = DefId> {
pub fn def_path_def_ids(tcx: TyCtxt<'_>, path: &[&str]) -> impl Iterator<Item = DefId> + use<> {
def_path_res(tcx, path).into_iter().filter_map(|res| res.opt_def_id())
}

Expand Down
24 changes: 12 additions & 12 deletions tests/ui/no_mangle_with_rust_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@
#![allow(unused)]
#![warn(clippy::no_mangle_with_rust_abi)]

#[no_mangle]
#[unsafe(no_mangle)]
fn rust_abi_fn_one(arg_one: u32, arg_two: usize) {}
//~^ ERROR: `#[no_mangle]` set on a function with the default (`Rust`) ABI
//~^ ERROR: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI
//~| NOTE: `-D clippy::no-mangle-with-rust-abi` implied by `-D warnings`

#[no_mangle]
#[unsafe(no_mangle)]
pub fn rust_abi_fn_two(arg_one: u32, arg_two: usize) {}
//~^ ERROR: `#[no_mangle]` set on a function with the default (`Rust`) ABI
//~^ ERROR: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI

/// # Safety
/// This function shouldn't be called unless the horsemen are ready
#[no_mangle]
#[unsafe(no_mangle)]
pub unsafe fn rust_abi_fn_three(arg_one: u32, arg_two: usize) {}
//~^ ERROR: `#[no_mangle]` set on a function with the default (`Rust`) ABI
//~^ ERROR: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI

/// # Safety
/// This function shouldn't be called unless the horsemen are ready
#[no_mangle]
#[unsafe(no_mangle)]
unsafe fn rust_abi_fn_four(arg_one: u32, arg_two: usize) {}
//~^ ERROR: `#[no_mangle]` set on a function with the default (`Rust`) ABI
//~^ ERROR: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI

#[no_mangle]
#[unsafe(no_mangle)]
fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines(
//~^ ERROR: `#[no_mangle]` set on a function with the default (`Rust`) ABI
//~^ ERROR: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI
arg_one: u32,
arg_two: usize,
) -> u32 {
0
}

// Must not run on functions that explicitly opt in to using the Rust ABI with `extern "Rust"`
#[no_mangle]
#[unsafe(no_mangle)]
#[rustfmt::skip]
extern "Rust" fn rust_abi_fn_explicit_opt_in(arg_one: u32, arg_two: usize) {}

fn rust_abi_fn_again(arg_one: u32, arg_two: usize) {}

#[no_mangle]
#[unsafe(no_mangle)]
extern "C" fn c_abi_fn(arg_one: u32, arg_two: usize) {}

extern "C" fn c_abi_fn_again(arg_one: u32, arg_two: usize) {}
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/no_mangle_with_rust_abi.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `#[no_mangle]` set on a function with the default (`Rust`) ABI
error: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI
--> tests/ui/no_mangle_with_rust_abi.rs:6:1
|
LL | fn rust_abi_fn_one(arg_one: u32, arg_two: usize) {}
Expand All @@ -15,7 +15,7 @@ help: or explicitly set the default
LL | extern "Rust" fn rust_abi_fn_one(arg_one: u32, arg_two: usize) {}
| +++++++++++++

error: `#[no_mangle]` set on a function with the default (`Rust`) ABI
error: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI
--> tests/ui/no_mangle_with_rust_abi.rs:11:1
|
LL | pub fn rust_abi_fn_two(arg_one: u32, arg_two: usize) {}
Expand All @@ -30,7 +30,7 @@ help: or explicitly set the default
LL | pub extern "Rust" fn rust_abi_fn_two(arg_one: u32, arg_two: usize) {}
| +++++++++++++

error: `#[no_mangle]` set on a function with the default (`Rust`) ABI
error: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI
--> tests/ui/no_mangle_with_rust_abi.rs:17:1
|
LL | pub unsafe fn rust_abi_fn_three(arg_one: u32, arg_two: usize) {}
Expand All @@ -45,7 +45,7 @@ help: or explicitly set the default
LL | pub unsafe extern "Rust" fn rust_abi_fn_three(arg_one: u32, arg_two: usize) {}
| +++++++++++++

error: `#[no_mangle]` set on a function with the default (`Rust`) ABI
error: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI
--> tests/ui/no_mangle_with_rust_abi.rs:23:1
|
LL | unsafe fn rust_abi_fn_four(arg_one: u32, arg_two: usize) {}
Expand All @@ -60,7 +60,7 @@ help: or explicitly set the default
LL | unsafe extern "Rust" fn rust_abi_fn_four(arg_one: u32, arg_two: usize) {}
| +++++++++++++

error: `#[no_mangle]` set on a function with the default (`Rust`) ABI
error: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI
--> tests/ui/no_mangle_with_rust_abi.rs:27:1
|
LL | / fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines(
Expand Down
57 changes: 57 additions & 0 deletions tests/ui/no_mangle_with_rust_abi_2021.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//@edition:2021
//
// Edition 2024 requires the use of #[unsafe(no_mangle)]

//@no-rustfix: overlapping suggestions
#![allow(unused)]
#![warn(clippy::no_mangle_with_rust_abi)]

#[no_mangle]
fn rust_abi_fn_one(arg_one: u32, arg_two: usize) {}
//~^ ERROR: `#[no_mangle]` set on a function with the default (`Rust`) ABI
//~| NOTE: `-D clippy::no-mangle-with-rust-abi` implied by `-D warnings`

#[no_mangle]
pub fn rust_abi_fn_two(arg_one: u32, arg_two: usize) {}
//~^ ERROR: `#[no_mangle]` set on a function with the default (`Rust`) ABI

/// # Safety
/// This function shouldn't be called unless the horsemen are ready
#[no_mangle]
pub unsafe fn rust_abi_fn_three(arg_one: u32, arg_two: usize) {}
//~^ ERROR: `#[no_mangle]` set on a function with the default (`Rust`) ABI

/// # Safety
/// This function shouldn't be called unless the horsemen are ready
#[no_mangle]
unsafe fn rust_abi_fn_four(arg_one: u32, arg_two: usize) {}
//~^ ERROR: `#[no_mangle]` set on a function with the default (`Rust`) ABI

#[no_mangle]
fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines(
//~^ ERROR: `#[no_mangle]` set on a function with the default (`Rust`) ABI
arg_one: u32,
arg_two: usize,
) -> u32 {
0
}

// Must not run on functions that explicitly opt in to using the Rust ABI with `extern "Rust"`
#[no_mangle]
#[rustfmt::skip]
extern "Rust" fn rust_abi_fn_explicit_opt_in(arg_one: u32, arg_two: usize) {}

fn rust_abi_fn_again(arg_one: u32, arg_two: usize) {}

#[no_mangle]
extern "C" fn c_abi_fn(arg_one: u32, arg_two: usize) {}

extern "C" fn c_abi_fn_again(arg_one: u32, arg_two: usize) {}

extern "C" {
fn c_abi_in_block(arg_one: u32, arg_two: usize);
}

fn main() {
// test code goes here
}
83 changes: 83 additions & 0 deletions tests/ui/no_mangle_with_rust_abi_2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
error: `#[no_mangle]` set on a function with the default (`Rust`) ABI
--> tests/ui/no_mangle_with_rust_abi_2021.rs:10:1
|
LL | fn rust_abi_fn_one(arg_one: u32, arg_two: usize) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::no-mangle-with-rust-abi` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::no_mangle_with_rust_abi)]`
help: set an ABI
|
LL | extern "C" fn rust_abi_fn_one(arg_one: u32, arg_two: usize) {}
| ++++++++++
help: or explicitly set the default
|
LL | extern "Rust" fn rust_abi_fn_one(arg_one: u32, arg_two: usize) {}
| +++++++++++++

error: `#[no_mangle]` set on a function with the default (`Rust`) ABI
--> tests/ui/no_mangle_with_rust_abi_2021.rs:15:1
|
LL | pub fn rust_abi_fn_two(arg_one: u32, arg_two: usize) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: set an ABI
|
LL | pub extern "C" fn rust_abi_fn_two(arg_one: u32, arg_two: usize) {}
| ++++++++++
help: or explicitly set the default
|
LL | pub extern "Rust" fn rust_abi_fn_two(arg_one: u32, arg_two: usize) {}
| +++++++++++++

error: `#[no_mangle]` set on a function with the default (`Rust`) ABI
--> tests/ui/no_mangle_with_rust_abi_2021.rs:21:1
|
LL | pub unsafe fn rust_abi_fn_three(arg_one: u32, arg_two: usize) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: set an ABI
|
LL | pub unsafe extern "C" fn rust_abi_fn_three(arg_one: u32, arg_two: usize) {}
| ++++++++++
help: or explicitly set the default
|
LL | pub unsafe extern "Rust" fn rust_abi_fn_three(arg_one: u32, arg_two: usize) {}
| +++++++++++++

error: `#[no_mangle]` set on a function with the default (`Rust`) ABI
--> tests/ui/no_mangle_with_rust_abi_2021.rs:27:1
|
LL | unsafe fn rust_abi_fn_four(arg_one: u32, arg_two: usize) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: set an ABI
|
LL | unsafe extern "C" fn rust_abi_fn_four(arg_one: u32, arg_two: usize) {}
| ++++++++++
help: or explicitly set the default
|
LL | unsafe extern "Rust" fn rust_abi_fn_four(arg_one: u32, arg_two: usize) {}
| +++++++++++++

error: `#[no_mangle]` set on a function with the default (`Rust`) ABI
--> tests/ui/no_mangle_with_rust_abi_2021.rs:31:1
|
LL | / fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines(
LL | |
LL | | arg_one: u32,
LL | | arg_two: usize,
LL | | ) -> u32 {
| |________^
|
help: set an ABI
|
LL | extern "C" fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines(
| ++++++++++
help: or explicitly set the default
|
LL | extern "Rust" fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines(
| +++++++++++++

error: aborting due to 5 previous errors

0 comments on commit 792f6ab

Please sign in to comment.