Skip to content

Commit

Permalink
Add machine applicability
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscerie committed Oct 15, 2023
1 parent c8df081 commit 5a52738
Show file tree
Hide file tree
Showing 33 changed files with 96 additions and 9 deletions.
5 changes: 4 additions & 1 deletion selene-lib/src/lint_filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
visit_nodes::{NodeVisitor, VisitorType},
},
lint_exists,
lints::{Diagnostic, Label, Severity},
lints::{Applicability, Diagnostic, Label, Severity},
CheckerDiagnostic, LintVariation,
};
use full_moon::{ast::Ast, node::Node, tokenizer::TokenType};
Expand Down Expand Up @@ -150,6 +150,7 @@ impl NodeVisitor for FilterVisitor {
trivia_end_position.bytes(),
)),
None,
Applicability::Unspecified,
))
}
}));
Expand Down Expand Up @@ -225,6 +226,7 @@ pub fn filter_diagnostics(
"global filter must be before this".to_owned(),
)],
None,
Applicability::Unspecified,
));

continue;
Expand All @@ -247,6 +249,7 @@ pub fn filter_diagnostics(
"conflicts with this".to_owned(),
)],
None,
Applicability::Unspecified,
));
}
}
Expand Down
26 changes: 26 additions & 0 deletions selene-lib/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ pub enum Severity {
Warning,
}

/// Indicates the confidence in the correctness of a suggestion
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Applicability {
/// The suggestion is definitely what the user intended, or maintains the exact meaning of the code
///
/// The suggestion is safe to be automatically applied
MachineApplicable,

/// The suggestion is probably what the user intended, but may not maintain the exact meaning of the code
///
/// The suggestion generates valid code when applied
MaybeIncorrect,

/// The suggestion is probably what the user intended, but may not maintain the exact meaning of the code
///
/// The suggestion does not generate valid code when applied
HasPlaceholders,

Unspecified,
}

#[derive(Debug)]
pub struct Diagnostic {
pub code: &'static str,
Expand All @@ -97,6 +118,7 @@ pub struct Diagnostic {
pub primary_label: Label,
pub secondary_labels: Vec<Label>,
pub fixed_code: Option<String>,
pub applicability: Applicability,
}

impl Diagnostic {
Expand All @@ -105,12 +127,14 @@ impl Diagnostic {
message: String,
primary_label: Label,
fixed_code: Option<String>,
applicability: Applicability,
) -> Self {
Self {
code,
message,
primary_label,
fixed_code,
applicability,

notes: Vec::new(),
secondary_labels: Vec::new(),
Expand All @@ -124,6 +148,7 @@ impl Diagnostic {
notes: Vec<String>,
secondary_labels: Vec<Label>,
fixed_code: Option<String>,
applicability: Applicability,
) -> Self {
Self {
code,
Expand All @@ -132,6 +157,7 @@ impl Diagnostic {
primary_label,
secondary_labels,
fixed_code,
applicability,
}
}

Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/almost_swapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl Lint for AlmostSwappedLint {
)],
Vec::new(),
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
6 changes: 6 additions & 0 deletions selene-lib/src/lints/bad_string_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ impl Lint for BadStringEscapeLint {
"string escape sequence doesn't exist".to_owned(),
Label::new(sequence.range.to_owned()),
None,
Applicability::Unspecified,
),
ReasonWhy::Malformed => Diagnostic::new(
"bad_string_escape",
"string escape sequence is malformed".to_owned(),
Label::new(sequence.range.to_owned()),
None,
Applicability::Unspecified,
),
ReasonWhy::DecimalTooHigh => Diagnostic::new_complete(
"bad_string_escape",
Expand All @@ -68,6 +70,7 @@ impl Lint for BadStringEscapeLint {
],
Vec::new(),
None,
Applicability::Unspecified,
),
ReasonWhy::CodepointTooHigh => Diagnostic::new_complete(
"bad_string_escape",
Expand All @@ -79,20 +82,23 @@ impl Lint for BadStringEscapeLint {
],
Vec::new(),
None,
Applicability::Unspecified,
),
ReasonWhy::DoubleInSingle => Diagnostic::new(
"bad_string_escape",
"double quotes do not have to be escaped when inside single quoted strings"
.to_owned(),
Label::new(sequence.range.to_owned()),
None,
Applicability::Unspecified,
),
ReasonWhy::SingleInDouble => Diagnostic::new(
"bad_string_escape",
"single quotes do not have to be escaped when inside double quoted strings"
.to_owned(),
Label::new(sequence.range.to_owned()),
None,
Applicability::Unspecified,
),
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/compare_nan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl Lint for CompareNanLint {
vec![format!("try: `{}` instead", fixed_code.clone())],
Vec::new(),
Some(fixed_code),
Applicability::MaybeIncorrect,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/constant_table_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl Lint for ConstantTableComparisonLint {
},
Vec::new(),
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ impl<'a> DeprecatedVisitor<'a> {
notes,
Vec::new(),
fixed_code,
Applicability::MachineApplicable,
));
}
}
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/divide_by_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Lint for DivideByZeroLint {
"dividing by zero is not allowed, use math.huge instead".to_owned(),
Label::new(*position),
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/duplicate_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl Lint for DuplicateKeysLint {
format!("`{}` originally declared here", duplicate.name),
)],
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/empty_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl Lint for EmptyIfLint {
.to_owned(),
Label::new(position.0),
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/empty_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl Lint for EmptyLoopLint {
"empty loop block".to_owned(),
Label::new(position),
Some(String::new()),
Applicability::MaybeIncorrect,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/global_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl Lint for GlobalLint {
),
Label::new(reference.identifier),
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/high_cyclomatic_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl Lint for HighCyclomaticComplexityLint {
),
Label::new(position),
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/if_same_then_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Lint for IfSameThenElseLint {
"note: same as this".to_owned(),
)],
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/ifs_same_cond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Lint for IfsSameCondLint {
"note: same as this".to_owned(),
)],
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/manual_table_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl ManualTableCloneMatch {
Vec::new()
},
None,
Applicability::Unspecified,
)
}
}
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/mismatched_arg_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl Lint for MismatchedArgCountLint {
})
.collect(),
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/mixed_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl Lint for MixedTableLint {
vec!["help: change this table to either an array or dictionary".to_owned()],
Vec::new(),
None,
Applicability::Unspecified,
));
}

Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/multiple_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl Lint for MultipleStatementsLint {
"only one statement per line is allowed".to_owned(),
Label::new(*position),
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/must_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl Lint for MustUseLint {
),
Label::new(function_call_stmt.call_prefix_range),
None,
Applicability::Unspecified,
));
}

Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/parenthese_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Lint for ParentheseConditionsLint {
"lua does not require parentheses around conditions".to_owned(),
Label::new(*position),
Some(context.code[position.0 + 1..position.1 - 1].to_string()),
Applicability::MachineApplicable,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/roblox_incorrect_color3_new_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Lint for Color3BoundsLint {
vec!["help: did you mean to use Color3.fromRGB instead?".to_owned()],
Vec::new(),
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
4 changes: 4 additions & 0 deletions selene-lib/src/lints/roblox_incorrect_roact_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl Lint for IncorrectRoactUsageLint {
),
Label::new(invalid_event.range),
None,
Applicability::Unspecified,
));
}

Expand All @@ -114,6 +115,7 @@ impl Lint for IncorrectRoactUsageLint {
)],
Vec::new(),
None,
Applicability::Unspecified,
));
}
_ => {
Expand All @@ -125,6 +127,7 @@ impl Lint for IncorrectRoactUsageLint {
),
Label::new(invalid_property.range),
None,
Applicability::Unspecified,
));
}
}
Expand All @@ -136,6 +139,7 @@ impl Lint for IncorrectRoactUsageLint {
format!("`{}` is not a valid class", unknown_class.name),
Label::new(unknown_class.range),
None,
Applicability::Unspecified,
));
}

Expand Down
9 changes: 8 additions & 1 deletion selene-lib/src/lints/roblox_suspicious_udim2_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ fn create_diagnostic(mismatch: &MismatchedArgCount) -> Diagnostic {
.to_owned()],
Vec::new(),
None,
Applicability::Unspecified,
)
} else {
Diagnostic::new(code, message, primary_label, None)
Diagnostic::new(
code,
message,
primary_label,
None,
Applicability::Unspecified,
)
}
}

Expand Down
1 change: 1 addition & 0 deletions selene-lib/src/lints/shadowing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl Lint for ShadowingLint {
"previously defined here".to_owned(),
)],
None,
Applicability::Unspecified,
)
})
.collect()
Expand Down
Loading

0 comments on commit 5a52738

Please sign in to comment.