Skip to content

Commit

Permalink
Fix clippy nursery lints.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenba committed Mar 30, 2024
1 parent 30dcdf7 commit 52e944e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
14 changes: 13 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ If you've noticed a bug or have a feature request then please raise a [new issue
It's generally best to check the [issues](https://github.com/kenba/cl3/issues) and [pull requests](https://github.com/kenba/cl3/pulls) (open and closed) to ensure that someone else has not noticed it before you. I recommend that you wait for confirmation of your bug or approval for your feature request in this way before starting to code.

Note: many OpenCL issues are hardware specific, so it is often useful to describe your setup, i.e.:

- `cl3` features, e.g. ["CL_VERSION_1_2", "CL_VERSION_2_0", "CL_VERSION_2_1", "CL_VERSION_2_1"] or default
- OpenCL target device vendor and version
- OpenCL ICD loader vendor and version
Expand All @@ -23,6 +24,7 @@ Please abide by our [Code of Conduct](CODE_OF_CONDUCT.md) in all issues and pull

If the issue is something you think that you can fix, then [fork cl3](https://docs.github.com/en/get-started/quickstart/fork-a-repo) and create a branch from `develop` with a descriptive name.
E.g. a good branch name would be (where issue #42 is the issue you're working on):

```shell
git checkout develop
git checkout -b 42-fix-some-bug
Expand All @@ -31,13 +33,17 @@ git checkout -b 42-fix-some-bug
## Get the test suite running

Run the unit tests:

```shell
cargo test -- --test-threads=1 --show-output
```

and integration tests:

```shell
cargo test -- --test-threads=1 --show-output --ignored
```

To ensure that you haven't broken anything.
Please feel free to add tests, especially where the new test(s) demonstrates a bug that you noticed.

Expand All @@ -52,24 +58,29 @@ Feel free to ask for help; everyone is a beginner at first.

Your patch should follow the same conventions & pass the same code quality checks as the rest of the project.
I recommend installing and running `clippy`:

```shell
cargo clippy
cargo clippy --all-features
```

## Make a Pull Request

At this point, you should switch back to your develop branch and make sure it's up to date with cl3's `develop` branch:

```shell
git remote add upstream [email protected]:kenba/cl3.git
git checkout develop
git pull upstream develop
```

Then update your feature branch from your local copy of master, and push it!

```shell
git checkout 42-fix-some-bug
git rebase master
git push --set-upstream origin 42-fix-some-bug
```

Finally, go to GitHub and make a [Pull Request](https://docs.github.com/en/github/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request).

Github Actions will then build your PR.
Expand All @@ -87,6 +98,7 @@ You should *not* introduce a fantastic new feature that you've just thought of!
If a maintainer asks you to "rebase" your PR, they're saying that a lot of code has changed, and that you need to update your branch so it's easier to merge.

Github have a good guide about [rebasing in Git](https://docs.github.com/en/get-started/using-git/about-git-rebase) here's our suggested workflow:

```shell
git checkout 42-fix-some-bug
git pull --rebase upstream develop
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,9 @@ thiserror = "1.0"
[[example]]
name = "clinfo"
path = "examples/clinfo.rs"

[lints.clippy]
enum_glob_use = "deny"
module_name_repetitions = "allow"
nursery = "deny"
unwrap_used = "deny"
6 changes: 3 additions & 3 deletions src/device.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Via Technology Ltd.
// Copyright (c) 2020-2024 Via Technology Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -742,7 +742,7 @@ pub const INTEL_DEVICE_VENDOR_ID: cl_uint = 0x8086;
pub const AMD_ON_APPLE_DEVICE_VENDOR_ID: cl_uint = 0x1021d00;

/// A text representation of an OpenCL vendor id.
pub fn vendor_id_text(vendor_id: cl_uint) -> &'static str {
pub const fn vendor_id_text(vendor_id: cl_uint) -> &'static str {
match vendor_id {
AMD_DEVICE_VENDOR_ID => "AMD",
IBM_DEVICE_VENDOR_ID => "IBM",
Expand All @@ -763,7 +763,7 @@ pub fn vendor_id_text(vendor_id: cl_uint) -> &'static str {

/// A text representation of an OpenCL device type, see:
/// [Device Types](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#device-types-table).
pub fn device_type_text(dev_type: cl_device_type) -> &'static str {
pub const fn device_type_text(dev_type: cl_device_type) -> &'static str {
match dev_type {
CL_DEVICE_TYPE_DEFAULT => "CL_DEVICE_TYPE_DEFAULT",
CL_DEVICE_TYPE_CPU => "CL_DEVICE_TYPE_CPU",
Expand Down
8 changes: 4 additions & 4 deletions src/error_codes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Via Technology Ltd.
// Copyright (c) 2020-2024 Via Technology Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,7 +55,7 @@ pub use opencl_sys::cl_egl::{CL_EGL_RESOURCE_NOT_ACQUIRED_KHR, CL_INVALID_EGL_OB
use std::fmt;
use thiserror::Error;

pub fn error_text(error_code: cl_int) -> &'static str {
pub const fn error_text(error_code: cl_int) -> &'static str {
match error_code {
CL_SUCCESS => "CL_SUCCESS",
CL_DEVICE_NOT_FOUND => "CL_DEVICE_NOT_FOUND",
Expand Down Expand Up @@ -171,7 +171,7 @@ pub struct ClError(pub cl_int);
/// Implement the From trait
impl From<cl_int> for ClError {
fn from(error: cl_int) -> Self {
ClError(error)
Self(error)
}
}

Expand All @@ -185,7 +185,7 @@ impl From<ClError> for &str {
/// Implement the From trait for String
impl From<ClError> for String {
fn from(error: ClError) -> Self {
String::from(error_text(error.0))
Self::from(error_text(error.0))
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Via Technology Ltd. All Rights Reserved.
// Copyright (c) 2020-2024 Via Technology Ltd. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -254,7 +254,7 @@ pub fn get_event_profiling_info(
}
}

pub fn status_text(status: cl_int) -> &'static str {
pub const fn status_text(status: cl_int) -> &'static str {
match status {
CL_COMPLETE => "CL_COMPLETE",
CL_RUNNING => "CL_RUNNING",
Expand All @@ -271,7 +271,7 @@ pub struct CommandExecutionStatus(pub cl_int);
/// Implement the From trait
impl From<cl_int> for CommandExecutionStatus {
fn from(status: cl_int) -> Self {
CommandExecutionStatus(status)
Self(status)
}
}

Expand All @@ -282,7 +282,7 @@ impl fmt::Display for CommandExecutionStatus {
}
}

pub fn command_type_text(command_type: cl_command_type) -> &'static str {
pub const fn command_type_text(command_type: cl_command_type) -> &'static str {
match command_type {
CL_COMMAND_NDRANGE_KERNEL => "CL_COMMAND_NDRANGE_KERNEL",
CL_COMMAND_TASK => "CL_COMMAND_TASK",
Expand Down Expand Up @@ -341,7 +341,7 @@ pub struct EventCommandType(pub cl_command_type);
/// Implement the From trait for EventCommandType
impl From<cl_command_type> for EventCommandType {
fn from(command_type: cl_command_type) -> Self {
EventCommandType(command_type)
Self(command_type)
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/info_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Via Technology Ltd.
// Copyright (c) 2020-2024 Via Technology Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -156,26 +156,26 @@ impl From<InfoType> for String {
let mut a = Vec::<u8>::from(info_type);

// remove all trailing nulls, if any
while let Some(0) = a.last() {
while a.last() == Some(&0) {
a.pop();
}

// convert invalid characters to std::char::REPLACEMENT_CHARACTER
String::from_utf8_lossy(&a).into_owned()
Self::from_utf8_lossy(&a).into_owned()
}
}

impl fmt::Display for InfoType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
InfoType::VecUchar(a) => {
Self::VecUchar(a) => {
let b = String::from_utf8_lossy(a).into_owned();
write!(f, "{}", b)
}

// Formats a LUID the same way as `clinfo`.
// See: https://github.com/Oblomov/clinfo/blob/master/src/clinfo.c
InfoType::Luid(a) => {
Self::Luid(a) => {
write!(
f,
"{:x}{:x}-{:x}{:x}{:x}{:x}{:x}{:x}",
Expand All @@ -184,7 +184,7 @@ impl fmt::Display for InfoType {
}

// Formats a UUID according to RFC4122.
InfoType::Uuid(a) => {
Self::Uuid(a) => {
write!(
f,
"{:x}{:x}{:x}{:x}-{:x}{:x}-{:x}{:x}-{:x}{:x}-{:x}{:x}{:x}{:x}{:x}{:x}",
Expand All @@ -207,7 +207,7 @@ impl fmt::Display for InfoType {
)
}

InfoType::VecNameVersion(a) => {
Self::VecNameVersion(a) => {
let mut s = String::default();
for b in a.iter() {
s.push('\n');
Expand All @@ -220,7 +220,7 @@ impl fmt::Display for InfoType {
write!(f, "{}", s)
}

InfoType::VecImageFormat(a) => {
Self::VecImageFormat(a) => {
let mut s = String::default();

for b in a.iter() {
Expand All @@ -236,7 +236,7 @@ impl fmt::Display for InfoType {

// Note: underlying type may not be a vector of Strings.
// If so use Debug trait instead
InfoType::VecVecUchar(a) => {
Self::VecVecUchar(a) => {
let mut s = String::default();
for b in a.iter() {
s.push('\n');
Expand Down

0 comments on commit 52e944e

Please sign in to comment.