Skip to content

Commit

Permalink
Fix a bunch of warnings, ignored lints and clippy lints new in nightly (
Browse files Browse the repository at this point in the history
  • Loading branch information
squell authored Oct 14, 2024
2 parents 1593913 + 29a9729 commit 7935463
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 50 deletions.
20 changes: 10 additions & 10 deletions src/common/bin_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,18 @@ mod tests {
assert_eq!(tx.read().unwrap(), 23);
}

#[test]
pub fn different_types() {
impl DeSerialize for u8 {
type Bytes = [u8; std::mem::size_of::<Self>()];
fn serialize(&self) -> [u8; 1] {
self.to_ne_bytes()
}
fn deserialize(bytes: [u8; 1]) -> Self {
Self::from_ne_bytes(bytes)
}
impl DeSerialize for u8 {
type Bytes = [u8; std::mem::size_of::<Self>()];
fn serialize(&self) -> [u8; 1] {
self.to_ne_bytes()
}
fn deserialize(bytes: [u8; 1]) -> Self {
Self::from_ne_bytes(bytes)
}
}

#[test]
pub fn different_types() {
let (mut tx, mut rx) = BinPipe::pair().unwrap();
tx.write(&42i32).unwrap();
assert_eq!(rx.read().unwrap(), 42);
Expand Down
19 changes: 9 additions & 10 deletions src/defaults/settings_dsl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
macro_rules! add_from {
($ctor:ident, $type:ty) => {
#[allow(non_local_definitions)]
impl From<$type> for $crate::defaults::SudoDefault {
fn from(value: $type) -> Self {
$crate::defaults::SudoDefault::$ctor(value.into())
Expand All @@ -9,8 +8,8 @@ macro_rules! add_from {
};

($ctor:ident, $type:ty, negatable$(, $vetting_function:expr)?) => {
#[allow(non_local_definitions)]
impl From<$type> for $crate::defaults::SudoDefault {
#[allow(clippy::from_str_radix_10)]
fn from(value: $type) -> Self {
$crate::defaults::SudoDefault::$ctor(OptTuple {
default: value.into(),
Expand All @@ -19,8 +18,8 @@ macro_rules! add_from {
}
}

#[allow(non_local_definitions)]
impl From<($type, $type)> for $crate::defaults::SudoDefault {
#[allow(clippy::from_str_radix_10)]
fn from((value, neg): ($type, $type)) -> Self {
$crate::defaults::SudoDefault::$ctor(OptTuple {
default: value.into(),
Expand Down Expand Up @@ -64,19 +63,19 @@ macro_rules! defaults {
$(stringify!($name)),*
];

add_from!(Flag, bool);
add_from!(Integer, i64, negatable, |text| i64::from_str_radix(text, 10).ok());
add_from!(Text, &'static str, negatable);
add_from!(Text, Option<&'static str>, negatable);
add_from!(List, &'static [&'static str]);
add_from!(Enum, StrEnum<'static>, negatable);

// because of the nature of radix and ranges, 'let mut result' is not always necessary, and
// a radix of 10 can also not always be avoided (and for uniformity, I would also not avoid this
// if this was hand-written code.
#[allow(unused_mut)]
#[allow(clippy::from_str_radix_10)]
pub fn sudo_default(var: &str) -> Option<SudoDefault> {
add_from!(Flag, bool);
add_from!(Integer, i64, negatable, |text| i64::from_str_radix(text, 10).ok());
add_from!(Text, &'static str, negatable);
add_from!(Text, Option<&'static str>, negatable);
add_from!(List, &'static [&'static str]);
add_from!(Enum, StrEnum<'static>, negatable);

Some(
match var {
$(stringify!($name) => {
Expand Down
2 changes: 1 addition & 1 deletion src/defaults/strenum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<'a> StrEnum<'a> {
}
}

impl<'a> std::ops::Deref for StrEnum<'a> {
impl std::ops::Deref for StrEnum<'_> {
type Target = str;
fn deref(&self) -> &str {
self.get()
Expand Down
4 changes: 2 additions & 2 deletions src/exec/use_pty/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ enum MonitorEvent {
ReadableBackchannel,
}

impl<'a> Process for MonitorClosure<'a> {
impl Process for MonitorClosure<'_> {
type Event = MonitorEvent;
type Break = io::Error;
type Exit = CommandStatus;
Expand All @@ -450,7 +450,7 @@ impl<'a> Process for MonitorClosure<'a> {
}
}

impl<'a> HandleSigchld for MonitorClosure<'a> {
impl HandleSigchld for MonitorClosure<'_> {
const OPTIONS: WaitOptions = WaitOptions::new().untraced().no_hang();

fn on_exit(&mut self, exit_code: c_int, registry: &mut EventRegistry<Self>) {
Expand Down
4 changes: 0 additions & 4 deletions src/exec/use_pty/pipe/ring_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ impl RingBuffer {
self.len == self.storage.len()
}

// rustc 1.77.1 clippy gives false diagnostics, https://github.com/rust-lang/rust-clippy/issues/12519
#[allow(clippy::unused_io_amount)]
pub(super) fn insert<R: Read>(&mut self, read: &mut R) -> io::Result<usize> {
let inserted_len = if self.is_empty() {
// Case 1.1. The buffer is empty, meaning that there are two unfilled slices in
Expand Down Expand Up @@ -63,8 +61,6 @@ impl RingBuffer {
self.len == 0
}

// rustc 1.77.1 clippy gives false diagnostics, https://github.com/rust-lang/rust-clippy/issues/12519
#[allow(clippy::unused_io_amount)]
pub(super) fn remove<W: Write>(&mut self, write: &mut W) -> io::Result<usize> {
let removed_len = if self.is_full() {
// Case 2.1. The buffer is full, meaning that there are two filled slices in `storage`:
Expand Down
8 changes: 0 additions & 8 deletions src/sudoers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ impl Sudo {
/// identifier = name
/// | #<numerical id>
/// ```

impl Parse for Identifier {
fn parse(stream: &mut impl CharStream) -> Parsed<Self> {
if accept_if(|c| c == '#', stream).is_some() {
Expand All @@ -215,7 +214,6 @@ impl Many for Identifier {}
///
/// This computes the correct negation with multiple exclamation marks in the parsing stage so we
/// are not bothered by it later.

impl<T: Parse + UserFriendly> Parse for Qualified<T> {
fn parse(stream: &mut impl CharStream) -> Parsed<Self> {
if is_syntax('!', stream)? {
Expand All @@ -242,7 +240,6 @@ impl<T: Many> Many for Qualified<T> {
}

/// Helper function for parsing `Meta<T>` things where T is not a token

fn parse_meta<T: Parse>(
stream: &mut impl CharStream,
embed: impl FnOnce(SudoString) -> T,
Expand All @@ -259,7 +256,6 @@ fn parse_meta<T: Parse>(
}

/// Since Identifier is not a token, add the parser for `Meta<Identifier>`

impl Parse for Meta<Identifier> {
fn parse(stream: &mut impl CharStream) -> Parsed<Self> {
parse_meta(stream, Identifier::Name)
Expand Down Expand Up @@ -365,7 +361,6 @@ impl Parse for MetaOrTag {
/// ```text
/// commandspec = [tag modifiers]*, command
/// ```

impl Parse for CommandSpec {
fn parse(stream: &mut impl CharStream) -> Parsed<Self> {
let mut tags = vec![];
Expand Down Expand Up @@ -414,7 +409,6 @@ impl Parse for CommandSpec {
/// ```text
/// (host,runas,commandspec) = hostlist, "=", [runas?, commandspec]+
/// ```

impl Parse for (SpecList<Hostname>, Vec<(Option<RunAs>, CommandSpec)>) {
fn parse(stream: &mut impl CharStream) -> Parsed<Self> {
let hosts = try_nonterminal(stream)?;
Expand Down Expand Up @@ -463,7 +457,6 @@ impl Many for (Option<RunAs>, CommandSpec) {}
/// ```
/// There is a syntactical ambiguity in the sudoer Directive and Permission specifications, so we
/// have to parse them 'together' and do a delayed decision on which category we are in.

impl Parse for Sudo {
// note: original sudo would reject:
// "User_Alias, user machine = command"
Expand Down Expand Up @@ -520,7 +513,6 @@ impl Parse for Sudo {
}

/// Parse the include/include dir part that comes after the '#' or '@' prefix symbol

fn parse_include(stream: &mut impl CharStream) -> Parsed<Sudo> {
fn get_path(stream: &mut impl CharStream) -> Parsed<String> {
if accept_if(|c| c == '"', stream).is_some() {
Expand Down
2 changes: 1 addition & 1 deletion src/sudoers/ast_names.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This module contains user-friendly names for the various items in the AST, to report in case they are missing
//! This module contains user-friendly names for the various items in the AST, to report in case they are missing

pub trait UserFriendly {
const DESCRIPTION: &'static str;
Expand Down
8 changes: 2 additions & 6 deletions src/sudoers/basic_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,11 @@ impl<T: Parse> Parse for Option<T> {
}

/// Parsing method for lists of items separated by a given character; this adheres to the contract of the [Parse] trait.
#[allow(clippy::multiple_bound_locations)]
pub(super) fn parse_list<T: Parse>(
pub(super) fn parse_list<T: Parse + UserFriendly>(
sep_by: char,
max: usize,
stream: &mut impl CharStream,
) -> Parsed<Vec<T>>
where
T: Parse + UserFriendly,
{
) -> Parsed<Vec<T>> {
let mut elems = Vec::new();
elems.push(try_nonterminal(stream)?);
while maybe(try_syntax(sep_by, stream))?.is_some() {
Expand Down
7 changes: 0 additions & 7 deletions src/sudoers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ pub(super) struct AliasTable {
}

/// A vector with a list defining the order in which it needs to be processed

type VecOrd<T> = (Vec<usize>, Vec<T>);

fn elems<T>(vec: &VecOrd<T>) -> impl Iterator<Item = &T> {
Expand All @@ -297,7 +296,6 @@ fn elems<T>(vec: &VecOrd<T>) -> impl Iterator<Item = &T> {
/// user/group. Not that in the sudoers file, later permissions override earlier restrictions.
/// The `cmdline` argument should already be ready to essentially feed to an exec() call; or be
/// a special command like 'sudoedit'.

// This code is structure to allow easily reading the 'happy path'; i.e. as soon as something
// doesn't match, we escape using the '?' mechanism.
fn check_permission<User: UnixUser + PartialEq<User>, Group: UnixGroup>(
Expand Down Expand Up @@ -339,7 +337,6 @@ fn check_permission<User: UnixUser + PartialEq<User>, Group: UnixGroup>(
/// Process a raw parsed AST bit of RunAs + Command specifications:
/// - RunAs specifications distribute over the commands that follow (until overridden)
/// - Tags accumulate over the entire line

fn distribute_tags(
runas_cmds: &[(Option<RunAs>, CommandSpec)],
) -> impl Iterator<Item = (Option<&RunAs>, (Tag, &Spec<Command>))> {
Expand All @@ -364,7 +361,6 @@ type FoundAliases = HashMap<String, bool>;
/// Find an item matching a certain predicate in an collection (optionally attributed) list of
/// identifiers; identifiers can be directly identifying, wildcards, and can either be positive or
/// negative (i.e. preceeded by an even number of exclamation marks in the sudoers file)

fn find_item<'a, Predicate, Iter, T: 'a>(
items: Iter,
matches: &Predicate,
Expand Down Expand Up @@ -487,7 +483,6 @@ fn unfold_alias_table<T>(table: &VecOrd<Def<T>>) -> HashMap<&String, &Vec<Qualif

/// Find all the aliases that a object is a member of; this requires [sanitize_alias_table] to have run first;
/// I.e. this function should not be "pub".

fn get_aliases<Predicate, T>(table: &VecOrd<Def<T>>, pred: &Predicate) -> FoundAliases
where
Predicate: Fn(&T) -> bool,
Expand All @@ -510,7 +505,6 @@ where
}

/// Code to map an ast::Identifier to the UnixUser trait

fn match_identifier(user: &impl UnixUser, ident: &ast::Identifier) -> bool {
match ident {
Identifier::Name(name) => user.has_name(name),
Expand Down Expand Up @@ -756,7 +750,6 @@ fn analyze(
/// Alias definition inin a Sudoers file can come in any order; and aliases can refer to other aliases, etc.
/// It is much easier if they are presented in a "definitional order" (i.e. aliases that use other aliases occur later)
/// At the same time, this is a good place to detect problems in the aliases, such as unknown aliases and cycles.

fn sanitize_alias_table<T>(table: &Vec<Def<T>>, diagnostics: &mut Vec<Error>) -> Vec<usize> {
fn remqualify<U>(item: &Qualified<U>) -> &U {
match item {
Expand Down
3 changes: 2 additions & 1 deletion src/system/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ mod test {
test_group(group(cstr!("daemon")), "daemon", 1);
}

impl UnixUser for () {}

#[test]
fn test_default() {
impl UnixUser for () {}
assert!(!().has_name("root"));
assert!(!().has_uid(0));
assert!(!().is_root());
Expand Down

0 comments on commit 7935463

Please sign in to comment.