Skip to content

Commit

Permalink
Fix clippy errors in sample code
Browse files Browse the repository at this point in the history
The original code masks flags with O_RDONLY, which is 0:
```
.read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
.write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))
```
This generates Clippy errors because it's equivalent to 0 & 0 != 0.

The correct code uses O_ACCMODE to mask off the lower bits:

```
.read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR))
.write((flags & O_ACCMODE == O_WRONLY) | (flags & O_ACCMODE == O_RDWR))
```

The code could be further simplified with `let access_flags = flags & O_ACCMODE` at the top, but this version maintains the original intent and consistency with similar libinput examples across the internet.

This commit updates all 3 occurrences of the code fragment.

Fixes Smithay#66

Signed-off-by: Tom Stokes <[email protected]>
  • Loading branch information
tomstokes committed Nov 8, 2024
1 parent 40b31d3 commit fe4c0c1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Configure and run event loop:

```rust
use input::{Libinput, LibinputInterface};
use libc::{O_RDONLY, O_RDWR, O_WRONLY};
use libc::{O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY};
use std::fs::{File, OpenOptions};
use std::os::unix::{fs::OpenOptionsExt, io::OwnedFd};
use std::path::Path;
Expand All @@ -46,8 +46,8 @@ impl LibinputInterface for Interface {
fn open_restricted(&mut self, path: &Path, flags: i32) -> Result<OwnedFd, i32> {
OpenOptions::new()
.custom_flags(flags)
.read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
.write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))
.read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR))
.write((flags & O_ACCMODE == O_WRONLY) | (flags & O_ACCMODE == O_RDWR))
.open(path)
.map(|file| file.into())
.map_err(|err| err.raw_os_error().unwrap())
Expand Down
6 changes: 3 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl Libinput {
/// # use std::fs::{File, OpenOptions};
/// # use std::os::unix::{fs::OpenOptionsExt, io::OwnedFd};
/// # use std::path::Path;
/// # use libc::{O_RDONLY, O_RDWR, O_WRONLY};
/// # use libc::{O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY};
/// #
/// use input::{Libinput, LibinputInterface};
/// use rustix::event::{poll, PollFlags, PollFd};
Expand All @@ -362,8 +362,8 @@ impl Libinput {
/// # fn open_restricted(&mut self, path: &Path, flags: i32) -> Result<OwnedFd, i32> {
/// # OpenOptions::new()
/// # .custom_flags(flags)
/// # .read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
/// # .write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))
/// # .read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR))
/// # .write((flags & O_ACCMODE == O_WRONLY) | (flags & O_ACCMODE == O_RDWR))
/// # .open(path)
/// # .map(|file| file.into())
/// # .map_err(|err| err.raw_os_error().unwrap())
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@
//! use std::path::Path;
//!
//! extern crate libc;
//! use libc::{O_RDONLY, O_RDWR, O_WRONLY};
//! use libc::{O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY};
//!
//! struct Interface;
//!
//! impl LibinputInterface for Interface {
//! fn open_restricted(&mut self, path: &Path, flags: i32) -> Result<OwnedFd, i32> {
//! OpenOptions::new()
//! .custom_flags(flags)
//! .read((flags & O_RDONLY != 0) | (flags & O_RDWR != 0))
//! .write((flags & O_WRONLY != 0) | (flags & O_RDWR != 0))
//! .read((flags & O_ACCMODE == O_RDONLY) | (flags & O_ACCMODE == O_RDWR))
//! .write((flags & O_ACCMODE == O_WRONLY) | (flags & O_ACCMODE == O_RDWR))
//! .open(path)
//! .map(|file| file.into())
//! .map_err(|err| err.raw_os_error().unwrap())
Expand Down

0 comments on commit fe4c0c1

Please sign in to comment.