Skip to content

Commit

Permalink
Use [lints] in Cargo.toml and apply more lints
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Oct 30, 2023
1 parent bfff5dd commit cbc62d2
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 76 deletions.
8 changes: 8 additions & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@

avoid-breaking-exported-api = false
disallowed-names = []
disallowed-macros = [
{ path = "std::dbg", reason = "it is okay to use during development, but please do not include it in main branch" },
]
disallowed-methods = [
{ path = "std::env::remove_var", reason = "this is not thread-safe and inherently unsafe; see <https://github.com/rust-lang/rust/issues/27970> for more" },
{ path = "std::env::set_var", reason = "this is not thread-safe and inherently unsafe; see <https://github.com/rust-lang/rust/issues/27970> for more" },
]
disallowed-types = []
msrv = "1.74" # nightly-2023-10-21 is 1.75 but not full 1.75 since it is nightly.
48 changes: 43 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ allowed_external_types = [
"futures_async_stream_macro::*",
]

[workspace]
resolver = "2"
members = ["futures-async-stream-macro", "tests/no-std", "tools/codegen"]

[lib]
doc-scrape-examples = false

Expand All @@ -37,4 +33,46 @@ pin-project = "1.0.11"
futures = { package = "futures-util", version = "0.3", default-features = false }
rustversion = "1"
static_assertions = "1"
trybuild = "1.0.49"
trybuild = { git = "https://github.com/taiki-e/trybuild.git", branch = "dev" } # adjust overwrite behavior

[lints]
workspace = true

[workspace]
resolver = "2"
members = ["futures-async-stream-macro", "tests/no-std", "tools/codegen"]

# This table is shared by projects under https://github.com/taiki-e.
# It is not intended for manual editing.
[workspace.lints.rust]
improper_ctypes = "warn"
improper_ctypes_definitions = "warn"
non_ascii_idents = "warn"
rust_2018_idioms = "warn"
single_use_lifetimes = "warn"
unreachable_pub = "warn"
unsafe_op_in_unsafe_fn = "warn"
[workspace.lints.clippy]
all = "warn" # Downgrade deny-by-default lints
pedantic = "warn"
as_ptr_cast_mut = "warn"
default_union_representation = "warn"
inline_asm_x86_att_syntax = "warn"
trailing_empty_array = "warn"
transmute_undefined_repr = "warn"
undocumented_unsafe_blocks = "warn"
# Suppress buggy or noisy clippy lints
borrow_as_ptr = { level = "allow", priority = 1 } # https://github.com/rust-lang/rust-clippy/issues/8286
doc_markdown = { level = "allow", priority = 1 }
float_cmp = { level = "allow", priority = 1 } # https://github.com/rust-lang/rust-clippy/issues/7725
manual_assert = { level = "allow", priority = 1 }
manual_range_contains = { level = "allow", priority = 1 } # https://github.com/rust-lang/rust-clippy/issues/6455#issuecomment-1225966395
missing_errors_doc = { level = "allow", priority = 1 }
module_name_repetitions = { level = "allow", priority = 1 }
similar_names = { level = "allow", priority = 1 }
single_match = { level = "allow", priority = 1 }
single_match_else = { level = "allow", priority = 1 }
struct_excessive_bools = { level = "allow", priority = 1 }
too_many_arguments = { level = "allow", priority = 1 }
too_many_lines = { level = "allow", priority = 1 }
type_complexity = { level = "allow", priority = 1 }
3 changes: 3 additions & 0 deletions futures-async-stream-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ proc-macro = true
proc-macro2 = "1.0.60"
quote = "1"
syn = { version = "2.0.1", features = ["full", "visit-mut"] }

[lints]
workspace = true
1 change: 0 additions & 1 deletion futures-async-stream-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
)
))]
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms, single_use_lifetimes, unreachable_pub, clippy::pedantic)]
#![feature(proc_macro_def_site)]

#[macro_use]
Expand Down
15 changes: 0 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,6 @@ where
)
))]
#![warn(
rust_2018_idioms,
single_use_lifetimes,
unreachable_pub,
clippy::pedantic,
// Lints that may help when writing public library.
missing_debug_implementations,
missing_docs,
Expand All @@ -283,16 +279,6 @@ where
clippy::missing_inline_in_public_items,
clippy::std_instead_of_alloc,
clippy::std_instead_of_core,
// Lints that may help when writing unsafe code.
improper_ctypes,
improper_ctypes_definitions,
unsafe_op_in_unsafe_fn,
clippy::as_ptr_cast_mut,
clippy::default_union_representation,
clippy::inline_asm_x86_att_syntax,
clippy::trailing_empty_array,
clippy::transmute_undefined_repr,
clippy::undocumented_unsafe_blocks,
)]
#![allow(clippy::must_use_candidate)]
#![feature(coroutine_trait)]
Expand Down Expand Up @@ -547,7 +533,6 @@ pub mod __private {
pub use core::future::Future;

#[doc(hidden)]
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/102352
pub use crate::future::{from_coroutine, get_context, ResumeTy};
}

Expand Down
7 changes: 0 additions & 7 deletions tests/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

#![cfg(not(miri))]
#![cfg(not(careful))]
#![warn(rust_2018_idioms, single_use_lifetimes)]

use std::env;

#[rustversion::attr(not(nightly), ignore)]
#[test]
fn ui() {
if env::var_os("CI").is_none() {
env::set_var("TRYBUILD", "overwrite");
}

let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/**/*.rs");
t.pass("tests/run-pass/**/*.rs");
Expand Down
1 change: 0 additions & 1 deletion tests/elisions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![warn(rust_2018_idioms, single_use_lifetimes)]
#![allow(clippy::needless_pass_by_value)]
#![allow(clippy::needless_lifetimes)] // broken
#![feature(coroutines)]
Expand Down
1 change: 0 additions & 1 deletion tests/for_await.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![warn(rust_2018_idioms, single_use_lifetimes)]
#![allow(clippy::semicolon_if_nothing_returned)] // broken
#![feature(coroutines, proc_macro_hygiene, stmt_expr_attributes)]

Expand Down
3 changes: 3 additions & 0 deletions tests/no-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ path = "lib.rs"
[dependencies]
futures-async-stream = { path = "../.." }
futures-core = { version = "0.3", default-features = false }

[lints]
workspace = true
2 changes: 1 addition & 1 deletion tests/no-std/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![no_std]
#![warn(rust_2018_idioms, single_use_lifetimes)]
#![feature(coroutines)]
#![allow(clippy::no_effect_underscore_binding)] // broken

use futures_async_stream::{stream, try_stream};

Expand Down
2 changes: 1 addition & 1 deletion tests/overwriting_core_crate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![warn(rust_2018_idioms, single_use_lifetimes)]
#![feature(coroutines)]
#![allow(clippy::no_effect_underscore_binding)] // broken

// See https://github.com/rust-lang/pin-utils/pull/26#discussion_r344491597
//
Expand Down
8 changes: 6 additions & 2 deletions tests/stream.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![warn(rust_2018_idioms, single_use_lifetimes)]
#![allow(clippy::unused_async)]
#![allow(clippy::needless_lifetimes, clippy::semicolon_if_nothing_returned)] // broken
#![allow(
clippy::must_use_candidate,
clippy::needless_lifetimes,
clippy::no_effect_underscore_binding,
clippy::semicolon_if_nothing_returned
)] // broken
#![feature(coroutines, proc_macro_hygiene, stmt_expr_attributes, gen_future)]

use std::{pin::Pin, rc::Rc, sync::Arc};
Expand Down
5 changes: 2 additions & 3 deletions tests/try_stream.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![warn(rust_2018_idioms, single_use_lifetimes)]
#![allow(clippy::try_err, clippy::unused_async)]
#![allow(clippy::semicolon_if_nothing_returned)] // broken
#![allow(unreachable_pub, clippy::try_err, clippy::unused_async)]
#![allow(clippy::semicolon_if_nothing_returned, clippy::no_effect_underscore_binding)] // broken
#![feature(coroutines, proc_macro_hygiene, stmt_expr_attributes)]

use futures::{
Expand Down
3 changes: 3 additions & 0 deletions tools/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ prettyplease = "0.2"
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full", "visit-mut"] }

[lints]
workspace = true
18 changes: 13 additions & 5 deletions tools/codegen/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ macro_rules! function_name {
}};
}

pub fn workspace_root() -> PathBuf {
pub(crate) fn workspace_root() -> PathBuf {
let mut dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
dir.pop(); // codegen
dir.pop(); // tools
dir
}

#[track_caller]
pub fn header(function_name: &str) -> String {
pub(crate) fn header(function_name: &str) -> String {
// rust-analyzer does not respect outer attribute (#[rustfmt::skip]) on
// a module without a body. So use inner attribute under cfg(rustfmt).
format!(
Expand All @@ -47,7 +47,11 @@ pub fn header(function_name: &str) -> String {
}

#[track_caller]
pub fn write(function_name: &str, path: impl AsRef<Path>, contents: TokenStream) -> Result<()> {
pub(crate) fn write(
function_name: &str,
path: impl AsRef<Path>,
contents: TokenStream,
) -> Result<()> {
write_raw(function_name, path.as_ref(), format_tokens(contents)?)
}

Expand Down Expand Up @@ -119,7 +123,11 @@ fn test_format_macros() {
}

#[track_caller]
pub fn write_raw(function_name: &str, path: &Path, contents: impl AsRef<[u8]>) -> Result<()> {
pub(crate) fn write_raw(
function_name: &str,
path: &Path,
contents: impl AsRef<[u8]>,
) -> Result<()> {
static LINGUIST_GENERATED: OnceLock<Vec<globset::GlobMatcher>> = OnceLock::new();
let linguist_generated = LINGUIST_GENERATED.get_or_init(|| {
let gitattributes = fs::read_to_string(workspace_root().join(".gitattributes")).unwrap();
Expand Down Expand Up @@ -148,7 +156,7 @@ pub fn write_raw(function_name: &str, path: &Path, contents: impl AsRef<[u8]>) -
Ok(())
}

pub fn git_ls_files(dir: &Path, filters: &[&str]) -> Result<Vec<(String, PathBuf)>> {
pub(crate) fn git_ls_files(dir: &Path, filters: &[&str]) -> Result<Vec<(String, PathBuf)>> {
let mut cmd = Command::new("git");
cmd.arg("ls-files").args(filters).current_dir(dir);
let output = cmd.output().with_context(|| format!("could not execute process `{cmd:?}`"))?;
Expand Down
33 changes: 14 additions & 19 deletions tools/codegen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![warn(rust_2018_idioms, single_use_lifetimes)]
#![allow(clippy::match_on_vec_items, clippy::needless_pass_by_value)]
#![allow(clippy::match_on_vec_items, clippy::needless_pass_by_value, clippy::wildcard_imports)]

#[macro_use]
mod file;
Expand Down Expand Up @@ -69,13 +68,11 @@ fn gen_assert_impl() -> Result<()> {
visited_types.insert(path_string.clone());

let has_generics = generics.type_params().count() != 0;
if generics.const_params().count() != 0 {
panic!(
"gen_assert_impl doesn't support const generics yet; \
skipped `{}`",
path_string
);
}
assert_eq!(
generics.const_params().count(),
0,
"gen_assert_impl doesn't support const generics yet; skipped `{path_string}`"
);

let lt = generics.lifetimes().map(|_| quote! { '_ }).collect::<Vec<_>>();
if has_generics {
Expand Down Expand Up @@ -170,10 +167,10 @@ fn gen_assert_impl() -> Result<()> {
});
}
} else {
let lt = if !lt.is_empty() {
quote! { <#(#lt),*> }
} else {
let lt = if lt.is_empty() {
quote! {}
} else {
quote! { <#(#lt),*> }
};
if NOT_SEND.contains(&path_string.as_str()) {
use_macros = true;
Expand Down Expand Up @@ -233,26 +230,24 @@ fn gen_assert_impl() -> Result<()> {
}

for &t in NOT_SEND {
assert!(visited_types.contains(t), "unknown type `{}` specified in NOT_SEND constant", t);
assert!(visited_types.contains(t), "unknown type `{t}` specified in NOT_SEND constant");
}
for &t in NOT_SYNC {
assert!(visited_types.contains(t), "unknown type `{}` specified in NOT_SYNC constant", t);
assert!(visited_types.contains(t), "unknown type `{t}` specified in NOT_SYNC constant");
}
for &t in NOT_UNPIN {
assert!(visited_types.contains(t), "unknown type `{}` specified in NOT_UNPIN constant", t);
assert!(visited_types.contains(t), "unknown type `{t}` specified in NOT_UNPIN constant");
}
for &t in NOT_UNWIND_SAFE {
assert!(
visited_types.contains(t),
"unknown type `{}` specified in NOT_UNWIND_SAFE constant",
t
"unknown type `{t}` specified in NOT_UNWIND_SAFE constant"
);
}
for &t in NOT_REF_UNWIND_SAFE {
assert!(
visited_types.contains(t),
"unknown type `{}` specified in NOT_REF_UNWIND_SAFE constant",
t
"unknown type `{t}` specified in NOT_REF_UNWIND_SAFE constant"
);
}

Expand Down
Loading

0 comments on commit cbc62d2

Please sign in to comment.