Skip to content

Commit

Permalink
Optimized the code according to Clippy's suggestions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-observer committed Jan 10, 2024
1 parent 3255a3b commit 0715127
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
42 changes: 14 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Backend {
})
.collect();

if dependency_with_versions.len() == 0 {
if dependency_with_versions.is_empty() {
return Vec::new();
}

Expand All @@ -61,36 +61,33 @@ impl Backend {
// Produce diagnostic hints for each crate where we might be helpful.
let diagnostics: Vec<_> = dependency_with_versions
.into_iter()
.filter_map(|dependency| {
.map(|dependency| {
if let Some(Some(newest_version)) = newest_packages.get(&dependency.name) {
match &dependency.version {
DependencyVersion::Complete { range, version } => {
if !version.matches(newest_version) {
return Some(Diagnostic::new_simple(
range.clone(),
Diagnostic::new_simple(
*range,
format!("{}: {newest_version}", &dependency.name),
));
)
} else {
let range = Range {
start: Position::new(range.start.line, 0),
end: Position::new(range.start.line, 0),
};

return Some(Diagnostic::new_simple(range, "✓".to_string()));
Diagnostic::new_simple(range, "✓".to_string())
}
}
DependencyVersion::Partial { range, .. } => {
return Some(Diagnostic::new_simple(
range.clone(),
format!("{}: {newest_version}", &dependency.name),
));
}
DependencyVersion::Partial { range, .. } => Diagnostic::new_simple(
*range,
format!("{}: {newest_version}", &dependency.name),
),
}
} else {
return Some(Diagnostic::new_simple(
Diagnostic::new_simple(
dependency.version.range(),
format!("{}: Unknown crate", &dependency.name),
));
)
}
})
.collect();
Expand Down Expand Up @@ -194,22 +191,11 @@ impl LanguageServer for Backend {
let Some(dependency) = dependencies
.into_iter()
.find(|dependency| match dependency {
Dependency::Partial { line, .. } => {
if *line == cursor.line {
true
} else {
false
}
}
Dependency::Partial { line, .. } => *line == cursor.line,
Dependency::WithVersion(dep) => {
if dep.version.range().start.line == cursor.line
dep.version.range().start.line == cursor.line
&& dep.version.range().start.character <= cursor.character
&& dep.version.range().end.character >= cursor.character
{
true
} else {
false
}
}
Dependency::Other { .. } => false,
})
Expand Down
6 changes: 4 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,17 @@ impl ManifestTracker {
if let Some(mut dependency) = Line::parse(line, i) {
if dependency
.name()
.and_then(|x| Some(x != "version"))
.map(|x| x != "version")
.unwrap_or_default()
{
continue;
} else {
// Rename to the package section, since the dependency is currently
// named "version" because of the Line::parse logic assuming this is
// a regular dependencies section.
dependency.name_mut().and_then(|x| Some(*x = name.clone()));
if let Some(x) = dependency.name_mut() {
*x = name.clone()
}
}
// Line::parse assumes line 0, modify so we have to fix this manually.
if let Some(version) = dependency.version_mut() {
Expand Down

0 comments on commit 0715127

Please sign in to comment.