Skip to content

Commit

Permalink
ci: run clippy with rust 1.75 (PLC-lang#1085)
Browse files Browse the repository at this point in the history
* ci: run clippy with rust 1.75

* Fix bitaccess test where true != true because of bitshift
  • Loading branch information
ghaith authored Feb 9, 2024
1 parent eeb0bab commit fdd22b4
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 48 deletions.
2 changes: 1 addition & 1 deletion compiler/plc_diagnostics/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ impl Diagnostic {

impl PartialOrd for Diagnostic {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.severity.partial_cmp(&other.severity)
Some(self.cmp(other))
}
}

Expand Down
4 changes: 1 addition & 3 deletions compiler/plc_driver/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,7 @@ impl CompileParameters {
}

pub fn get_config_options(&self) -> Option<(ConfigOption, ConfigFormat)> {
let Some(SubCommands::Config { format, option }) = &self.commands else {
return None
};
let Some(SubCommands::Config { format, option }) = &self.commands else { return None };
Some((*option, *format))
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/plc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ fn get_project(compile_parameters: &CompileParameters) -> Result<Project<PathBuf
//Build with parameters
let name = compile_parameters
.input
.get(0)
.first()
.and_then(|it| it.get_location())
.and_then(|it| it.file_name())
.and_then(|it| it.to_str())
Expand Down
2 changes: 1 addition & 1 deletion compiler/plc_xml/src/model/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Interface {
}

pub fn get_data_content(&self) -> Option<&str> {
let Some(ref data) = self.add_data else { return None };
let data = self.add_data.as_ref()?;

Some(&data.content)
}
Expand Down
13 changes: 9 additions & 4 deletions compiler/plc_xml/src/xml_parser/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ fn get_connection(
let Some(ref_local_id) = control.ref_local_id else {
let location =
session.range_factory.create_block_location(control.local_id, control.execution_order_id);
return Err(Diagnostic::error("Control statement has no connection").with_error_code("E081").with_location(location));
return Err(Diagnostic::error("Control statement has no connection")
.with_error_code("E081")
.with_location(location));
};

let Some(node) = index.get(&ref_local_id) else {
let location = session.range_factory.create_block_location(ref_local_id, None);
return Err(Diagnostic::error(format!("Node {} is referencing a non-existing element with ID {ref_local_id}",control.local_id))
.with_error_code("E082")
.with_location(location));
return Err(Diagnostic::error(format!(
"Node {} is referencing a non-existing element with ID {ref_local_id}",
control.local_id
))
.with_error_code("E082")
.with_location(location));
};

match node {
Expand Down
4 changes: 2 additions & 2 deletions compiler/plc_xml/src/xml_parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ fn ast_generates_locations() {
&SInVariable::id(5).with_expression("1"),
]);

let source_code = SourceCode::new(&content.serialize(), "<internal>.cfc");
let source_code = SourceCode::new(content.serialize(), "<internal>.cfc");
let (units, diagnostics) = xml_parser::parse(&source_code, LinkageType::Internal, IdProvider::default());
let impl1 = &units.implementations[0];
//Deconstruct assignment and get locations
Expand Down Expand Up @@ -366,7 +366,7 @@ fn ast_diagnostic_locations() {
&SOutVariable::id(2).with_execution_id(0).with_expression("a").connect(1), // "a" isn't declared anywhere, hence the error
]);

let source_code = SourceCode::new(&content.serialize(), "<internal>.cfc");
let source_code = SourceCode::new(content.serialize(), "<internal>.cfc");
let (units, diagnostics) = xml_parser::parse(&source_code, LinkageType::Internal, IdProvider::default());
let impl1 = &units.implementations[0];
assert_debug_snapshot!(impl1);
Expand Down
4 changes: 2 additions & 2 deletions libs/stdlib/src/date_time_extra_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub extern "C" fn SPLIT_DATE__UDINT(in1: i64, out1: &mut u32, out2: &mut u32, ou
pub extern "C" fn SPLIT_DATE__LINT(in1: i64, out1: &mut i64, out2: &mut i64, out3: &mut i64) -> i16 {
let date = chrono::Utc.timestamp_nanos(in1).date_naive();
// if year does not fit in target data type -> panic
*out1 = date.year().try_into().unwrap();
*out1 = date.year().into();
*out2 = date.month() as i64;
*out3 = date.day() as i64;

Expand Down Expand Up @@ -516,7 +516,7 @@ pub extern "C" fn SPLIT_DT__LINT(
) -> i16 {
let dt = chrono::Utc.timestamp_nanos(in1);
// if year does not fit in target data type -> panic
*out1 = dt.year().try_into().unwrap();
*out1 = dt.year().into();
*out2 = dt.month() as i64;
*out3 = dt.day() as i64;
*out4 = dt.hour() as i64;
Expand Down
16 changes: 8 additions & 8 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ lazy_static! {
let Some(params) = parameters else { return; };
// Get the input and annotate it with a pointer type
let input = flatten_expression_list(params);
let Some(input) = input.get(0) else { return; };
let Some(input) = input.first() else { return; };
let input_type = annotator.annotation_map
.get_type_or_void(input, annotator.index)
.get_type_information()
Expand Down Expand Up @@ -136,13 +136,13 @@ lazy_static! {
let context = llvm.context;
let builder = &llvm.builder;

let function_context = generator.get_function_context(params.get(0).expect("Param 0 exists"))?;
let function_context = generator.get_function_context(params.first().expect("Param 0 exists"))?;
let insert_block = builder.get_insert_block().expect("Builder should have a block at this point");

//Generate an access from the first param
if let (&[k], params) = params.split_at(1) {
//Create a temp var
let result_type = params.get(0)
let result_type = params.first()
.ok_or_else(|| Diagnostic::codegen_error("Invalid signature for MUX", location))
.and_then(|it| generator.get_type_hint_info_for(it))
.and_then(|it| generator.llvm_index.get_associated_type(it.get_name()))?;
Expand Down Expand Up @@ -642,7 +642,7 @@ fn annotate_comparison_function(
)
})
.collect::<Vec<_>>();
let Some(new_statement) = comparisons.get(0) else {
let Some(new_statement) = comparisons.first() else {
// no windows => less than 2 parameters, caught during validation
return;
};
Expand Down Expand Up @@ -688,7 +688,7 @@ fn annotate_arithmetic_function(
let find_biggest_param_type_name = |annotator: &TypeAnnotator| {
let mut bigger = annotator
.annotation_map
.get_type_or_void(params_flattened.get(0).expect("must have this parameter"), annotator.index);
.get_type_or_void(params_flattened.first().expect("must have this parameter"), annotator.index);

for param in params_flattened.iter().skip(1) {
let right_type = annotator.annotation_map.get_type_or_void(param, annotator.index);
Expand All @@ -702,7 +702,7 @@ fn annotate_arithmetic_function(

// create nested AstStatement::BinaryExpression for each parameter, such that
// ADD(a, b, c, d) ends up as (((a + b) + c) + d)
let left = (*params_flattened.get(0).expect("Must exist")).clone();
let left = (*params_flattened.first().expect("Must exist")).clone();
let new_statement = params_flattened.into_iter().skip(1).fold(left, |left, right| {
AstFactory::create_binary_expression(left, operation, right.clone(), ctx.id_provider.next_id())
});
Expand All @@ -722,7 +722,7 @@ fn annotate_variable_length_array_bound_function(
return;
};
let params = ast::flatten_expression_list(parameters);
let Some(vla) = params.get(0) else {
let Some(vla) = params.first() else {
// caught during validation
return;
};
Expand Down Expand Up @@ -762,7 +762,7 @@ fn validate_variable_length_array_bound_function(
));
}

match (params.get(0), params.get(1)) {
match (params.first(), params.get(1)) {
(Some(vla), Some(idx)) => {
let idx_type = annotations.get_type_or_void(idx, index);

Expand Down
1 change: 1 addition & 0 deletions src/codegen/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ impl<'ink> DebugBuilder<'ink> {
inner_type.into(),
size.bits().into(),
alignment.bits(),
#[allow(clippy::single_range_in_vec_init)]
&[(0..(length - 1))],
);
self.register_concrete_type(name, DebugType::Composite(array_type));
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/generators/expression_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2393,13 +2393,13 @@ impl<'ink, 'b> ExpressionCodeGenerator<'ink, 'b> {
// relatively straightforward for single-dimensional arrays, but is quite costly (O(n²)) for multi-dimensional arrays
let access_statements = access.get_as_list();
let accessor = if access_statements.len() == 1 {
let Some(stmt) = access_statements.get(0) else {
let Some(stmt) = access_statements.first() else {
unreachable!("Must have exactly 1 access statement")
};
let access_value = self.generate_expression(stmt).map_err(|_| ())?;

// if start offset is not 0, adjust the access value accordingly
let Some(start_offset) = index_offsets.get(0).map(|(start, _)| *start) else {
let Some(start_offset) = index_offsets.first().map(|(start, _)| *start) else {
unreachable!("VLA must have information about dimension offsets")
};
self.create_llvm_int_binary_expression(&Operator::Minus, access_value, start_offset.into())
Expand Down Expand Up @@ -2581,7 +2581,7 @@ pub fn get_implicit_call_parameter<'a>(
//TODO: use global context to get an expression slice
Diagnostic::error("Expression is not assignable")
.with_error_code("E050")
.with_location(param_statement.get_location())
.with_location(param_statement.get_location()),
);
};
let loc = declared_parameters
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/generators/statement_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl<'a, 'b> StatementCodeGenerator<'a, 'b> {
)
} else {
//TODO: using the global context we could get a slice here
Err(Diagnostic::error(&format!("{element:?} not a direct access"))
Err(Diagnostic::error(format!("{element:?} not a direct access"))
.with_error_code("E055")
.with_location(element.get_location()))
}?;
Expand Down
9 changes: 3 additions & 6 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,13 +1115,10 @@ impl Index {
segments
.iter()
.skip(1)
.fold(Some((segments[0], init)), |accum, current| match accum {
Some((_, Some(context))) => {
Some((*current, self.find_member(&context.data_type_name, current)))
}
.try_fold((segments[0], init), |accum, current| match accum {
(_, Some(context)) => Some((*current, self.find_member(&context.data_type_name, current))),
// The variable could be in a block that has no global variable (Function block)
Some((name, None)) => Some((*current, self.find_member(name, current))),
None => Some((*current, self.find_global_variable(current))),
(name, None) => Some((*current, self.find_member(name, current))),
})
.and_then(|(_, it)| it)
}
Expand Down
2 changes: 1 addition & 1 deletion src/index/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ where
where
Q: Hash + Equivalent<K>,
{
self.get_all(key).and_then(|it| it.get(0))
self.get_all(key).and_then(|it| it.first())
}

/// returns all elements associated with the given key or None if
Expand Down
6 changes: 3 additions & 3 deletions src/index/tests/index_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ fn pou_duplicates_are_indexed() {
//THEN I expect both PouIndexEntries
let pous = index.get_pous().values().filter(|it| it.get_name().eq("foo")).collect::<Vec<_>>();

let foo1 = pous.get(0).unwrap();
let foo1 = pous.first().unwrap();
assert_eq!(foo1.get_name(), "foo");

let foo2 = pous.get(1).unwrap();
Expand Down Expand Up @@ -1566,7 +1566,7 @@ fn type_duplicates_are_indexed() {
//THEN I expect all 3 DataTypes
let types = index.get_types().values().filter(|it| it.get_name().eq("MyStruct")).collect::<Vec<_>>();

let mystruct1 = types.get(0).unwrap();
let mystruct1 = types.first().unwrap();
assert_eq!(mystruct1.get_name(), "MyStruct");

let mystruct2 = types.get(1).unwrap();
Expand Down Expand Up @@ -1595,7 +1595,7 @@ fn global_variables_duplicates_are_indexed() {
//THEN I expect both globals
let globals = index.get_globals().values().filter(|it| it.get_name().eq("x")).collect::<Vec<_>>();

let x1 = globals.get(0).unwrap();
let x1 = globals.first().unwrap();
assert_eq!(x1.get_name(), "x");
assert_eq!(x1.get_type_name(), "INT");

Expand Down
2 changes: 1 addition & 1 deletion src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,7 @@ impl<'i> TypeAnnotator<'i> {
if let DataTypeInformation::Pointer { inner_type_name, .. } = &self
.index
.get_effective_type_or_void_by_name(
members.get(0).expect("internal VLA struct ALWAYS has this member").get_type_name(),
members.first().expect("internal VLA struct ALWAYS has this member").get_type_name(),
)
.get_type_information()
{
Expand Down
2 changes: 1 addition & 1 deletion src/resolver/const_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn get_default_initializer(
Some(AstFactory::create_literal(AstLiteral::new_integer(0), location.clone(), id))
}
DataTypeInformation::Enum { name, elements, .. } => elements
.get(0)
.first()
.and_then(|default_enum| index.find_enum_element(name, default_enum))
.and_then(|enum_element| enum_element.initial_value)
.and_then(|initial_val| {
Expand Down
8 changes: 4 additions & 4 deletions src/validation/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ pub fn visit_variable<T: AnnotationMap>(
/// - InOut within Function-Block
fn validate_vla(validator: &mut Validator, pou: Option<&Pou>, block: &VariableBlock, variable: &Variable) {
let Some(pou) = pou else {

if matches!(block.variable_block_type, VariableBlockType::Global) {
validator.push_diagnostic(Diagnostic::error("VLAs can not be defined as global variables")
.with_error_code("E044")
.with_location( variable.location.clone())
validator.push_diagnostic(
Diagnostic::error("VLAs can not be defined as global variables")
.with_error_code("E044")
.with_location(variable.location.clone()),
)
}

Expand Down
12 changes: 6 additions & 6 deletions tests/correctness/bitaccess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use pretty_assertions::assert_eq;
struct MainType {
variable: u64,
access_var: i16,
bit_target: bool,
bit_target2: bool,
bit_target: u8, //bool
bit_target2: u8, //bool
byte_target: u8,
word_target: u16,
dword_target: u32,
Expand Down Expand Up @@ -125,11 +125,11 @@ fn bitaccess_test() {
MainType {
variable: 0xAB_CD_EF_12_34_56_78_90,
access_var: 0,
bit_target: true,
bit_target: 1,
bit_target2: 85, //variable >> 32 >> 16 >> 8 >> 1 = 85
byte_target: 0xAB,
word_target: 0xAB_CD,
dword_target: 0xAB_CD_EF_12,
bit_target2: true,
}
)
}
Expand Down Expand Up @@ -168,11 +168,11 @@ fn bitaccess_with_var_test() {
MainType {
variable: 0xAB_CD_EF_12_34_56_78_90,
access_var: 1,
bit_target: true,
bit_target: 1,
bit_target2: 85, //variable >> 32 >> 16 >> 8 >> 1 = 85
byte_target: 0xAB,
word_target: 0xAB_CD,
dword_target: 0xAB_CD_EF_12,
bit_target2: true,
}
)
}
Expand Down

0 comments on commit fdd22b4

Please sign in to comment.